Javascript 在ng repeat中添加新指令

Javascript 在ng repeat中添加新指令,javascript,angularjs,Javascript,Angularjs,我有一个数据数组,在ng repeat中使用指令显示。当用户单击一个按钮时,它将在此数组中添加一个新对象,因此必须执行该指令的一个新实例。我目前的HTML: <ul> <li ng-repeat="c in commissions" commission="c.commission" index="{{$index}}"></li> </ul> 我的指示: var Commission; Commission = function() {

我有一个数据数组,在ng repeat中使用指令显示。当用户单击一个按钮时,它将在此数组中添加一个新对象,因此必须执行该指令的一个新实例。我目前的HTML:

<ul>
  <li ng-repeat="c in commissions" commission="c.commission" index="{{$index}}"></li>
</ul>
我的指示:

var Commission;

Commission = function() {
  return {
    restrict: 'A',
    replace: true,
    templateUrl: '/assets/template/commission/list.html',
    controller: 'ProductEditCtrl',
    scope: {
      commission: '=',
      index: '@'
    },
    link: function(scope, el, attrs, ctrl) {
      var commission;
      commission = scope.commission;
      commission.index = scope.index;
      scope.editable = false;
      scope.changeType = function(type) {
        return commission.type = type;
      };
      scope.removeCommission = function() {
        return ctrl.deleteCommission(commission.index, commission.id);
      };
      scope.saveCommission = function() {
        var obj;
        obj = {
          seller_id: commission.seller.id,
          type: commission.type,
          value: commission.value
        };
        ctrl.changeCommission(commission.id, obj);
        return scope.editable = false;
      };
      return scope.turnEditable = function() {
        return scope.editable = true;
      };
    }
  };
};
角度模块(“horus.products”)。指令(“委员会”,委员会)

因此,发生的情况是,一个新的添加到我的列表中,它根据我的指令编译模板,但它给了我一个错误:

TypeError:无法设置未定义的属性“index” 在链接()处 在 在nodeLinkFn() 在delayedNodeLinkFn() 在kfn()处 在publicLinkFn() 在boundTranscludeFn()处 在controllersBoundTransclude()处 在下一步行动() at对象$watchCollectionAction[作为fn]()


知道为什么会发生这种情况吗?我该如何解决?我这样做是在ng repeat中添加新指令的最佳方法吗?

我找到了解决我的错误的方法,它是由我定义插入数组的对象的方式的问题引起的。。因此dumb haha

该指令是一个没有值的属性,只是一个将元素绑定到具体类型的标志
var Commission;

Commission = function() {
  return {
    restrict: 'A',
    replace: true,
    templateUrl: '/assets/template/commission/list.html',
    controller: 'ProductEditCtrl',
    scope: {
      commission: '=',
      index: '@'
    },
    link: function(scope, el, attrs, ctrl) {
      var commission;
      commission = scope.commission;
      commission.index = scope.index;
      scope.editable = false;
      scope.changeType = function(type) {
        return commission.type = type;
      };
      scope.removeCommission = function() {
        return ctrl.deleteCommission(commission.index, commission.id);
      };
      scope.saveCommission = function() {
        var obj;
        obj = {
          seller_id: commission.seller.id,
          type: commission.type,
          value: commission.value
        };
        ctrl.changeCommission(commission.id, obj);
        return scope.editable = false;
      };
      return scope.turnEditable = function() {
        return scope.editable = true;
      };
    }
  };
};