我有一個數組數組,它使用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