我有一个数组数组,它使用ng-repeat内的指令显示。当用户点击一个按钮时,它会在这个数组中添加一个新的对象,所以必须执行该指令的新实例。我此刻的HTML:在ng-repeat中添加新指令
<ul>
<li ng-repeat="c in commissions" commission="c.commission" index="{{$index}}"></li>
</ul>
当用户点击 “添加” 按钮,执行以下功能(CoffeeScript的):
$scope.newCommission = function() {
var commission;
commission = {
new_commission: true
};
return $scope.commissions.push(commission);
};
我的指令,ATM:
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;
};
}
};
};
angular.module('horus.products')。指令('commission',Commission);
那么,会发生什么情况是这样的,一个新加入到我的列表,并将其汇集了来自我的指令中的模板,但它给了我一个错误:
TypeError: Cannot set property 'index' of undefined at link (http://horus.dev/assets/app/modules/products/directives/commission.js?body=1:17:26) at http://horus.dev/assets/angular/angular.js?body=1:7036:44 at nodeLinkFn (http://horus.dev/assets/angular/angular.js?body=1:6634:13) at delayedNodeLinkFn (http://horus.dev/assets/angular/angular.js?body=1:6856:11) at compositeLinkFn (http://horus.dev/assets/angular/angular.js?body=1:6029:13) at publicLinkFn (http://horus.dev/assets/angular/angular.js?body=1:5924:30) at boundTranscludeFn (http://horus.dev/assets/angular/angular.js?body=1:6049:21) at controllersBoundTransclude (http://horus.dev/assets/angular/angular.js?body=1:6655:18) at ngRepeatAction (http://horus.dev/assets/angular/angular.js?body=1:20345:15) at Object.$watchCollectionAction [as fn] (http://horus.dev/assets/angular/angular.js?body=1:12269:13)
任何想法,为什么发生这种情况,如何能我解决这个问题?我是否正在做这种在ng-repeat中添加新指令的最佳方法?
该指令是没有值的属性造成的<...委托...>只是一个将元素绑定到具体类型的标志 – 2014-09-03 13:04:30