admin管理员组

文章数量:1279188

fiddle here /

I have data from json like so

$scope.info={
 "pany1":"this",
 "pany2":"is",
  "pany3":"sparta"
}

I am using ng-repeat to print all the data and I want to monotor for changes on the fields.

 <input type="text" ng-repeat="item in info" value="{{item}}" monitor-change>

I have a monitorChange directive like this:

.directive('monitorChange', function() {
  return {
    restrict: 'A',
    scope: {changedFlag: '='},
    link: function(scope, element, attrs) {
      var $el = angular.element(element);
      $el.on('keyup', function() {//bind to element
          scope.$apply( function() {
            scope.changedFlag =true;//on key press value is changed
          });
      });
    }
  };
}); 

When trying to change the data, I receive the error Error: [$pile:nonassign] Expression 'undefined' used with directive 'monitorChange' is non-assignable!

I am printing the data in my view with:

{{changedFlag }}

What is wrong with the code?

fiddle here http://jsfiddle/prantikv/dJty6/36/

I have data from json like so

$scope.info={
 "pany1":"this",
 "pany2":"is",
  "pany3":"sparta"
}

I am using ng-repeat to print all the data and I want to monotor for changes on the fields.

 <input type="text" ng-repeat="item in info" value="{{item}}" monitor-change>

I have a monitorChange directive like this:

.directive('monitorChange', function() {
  return {
    restrict: 'A',
    scope: {changedFlag: '='},
    link: function(scope, element, attrs) {
      var $el = angular.element(element);
      $el.on('keyup', function() {//bind to element
          scope.$apply( function() {
            scope.changedFlag =true;//on key press value is changed
          });
      });
    }
  };
}); 

When trying to change the data, I receive the error Error: [$pile:nonassign] Expression 'undefined' used with directive 'monitorChange' is non-assignable!

I am printing the data in my view with:

{{changedFlag }}

What is wrong with the code?

Share Improve this question edited Mar 16, 2015 at 13:06 Claies 22.3k4 gold badges55 silver badges80 bronze badges asked Mar 16, 2015 at 12:58 krvkrv 2,9408 gold badges45 silver badges83 bronze badges 2
  • You should post the actual error message you are receiving. I'll update the question with the full error for you. – Claies Commented Mar 16, 2015 at 13:02
  • 2 scope: {changedFlag: '='}, but you're not declaring in the HTML any changed-flag attribute. – Omri Aharon Commented Mar 16, 2015 at 13:07
Add a ment  | 

1 Answer 1

Reset to default 9
  1. As you mentioned scope: {caretPosition: '='} in directive definition, we need to pass caret-position="obj.changedFlag" in the markup.
  2. As ng-repeat creates a new scope for each item, it is good to use the Dot notation for the changes to reflect in the controller's scope.

Here is the updated fiddle. http://jsfiddle/dJty6/38/

本文标签: javascriptError Expression 39undefined39 used with directive is nonassignableStack Overflow