错误:与指令一起使用的表达式“未定义”不可赋值

Error: Expression 'undefined' used with directive is non-assignable

本文关键字:未定义 赋值 表达式 指令 一起 错误      更新时间:2023-09-26

在这里小提琴 http://jsfiddle.net/prantikv/dJty6/36/

我有来自 json 的数据,就像这样

$scope.info={
 "company1":"this",
 "company2":"is",
  "company3":"sparta"
}

我正在使用ng-repeat来打印所有数据,并且我想对字段进行更改。

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

我有一个这样的监视器更改指令:

.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
          });
      });
    }
  };
}); 

尝试更改数据时,我收到错误Error: [$compile:nonassign] Expression 'undefined' used with directive 'monitorChange' is non-assignable!

我正在打印视图中的数据:

{{changedFlag }}

代码有什么问题?

  1. 正如您在指令定义中scope: {caretPosition: '='}提到的,我们需要在标记中传递caret-position="obj.changedFlag"
  2. 由于ng-repeat为每个项目创建一个新范围,因此最好使用点表示法使更改反映在控制器的作用域中。

这是更新的小提琴。http://jsfiddle.net/dJty6/38/