我有一個具有多個屬性的指令。其中一個屬性用於將數組值綁定到指令。我console.log(scope)
在link
功能,並看到它的屬性附加,但它是未定義的。它有屬性countObj:undefined
傳遞給指令的數組值在ng-repeat中是未定義的
該陣列基本上包含:countArray= [0,1..]
我想在數組的值增加0到指令1和1至指令2等。值傳遞到下一個編號(2如果最後一個值是1)如果創建了新的指令。我正在使用ng-repeat
來顯示指令並在需要新指令時增加數組(0,1,2 ..)。我想將各自的數組值傳遞給每個指令。
首先,默認情況下我有$ scope.countArray = [0],所以ng-repeat
有一個值,默認顯示一個指令。所以至少第一個指令的console.log(scope.countObj)
應該包含一個值,而不是undefined
。
這是使用該指令的html:
<month-dir ng-repeat="count in countArray" countObj="countArray[count]" on-cl="removeDirCtrl(removeVal)" obj="ebObj.costArray[count]">
<ng-transclude></ng-transclude>
</month-dir>
屬性countObj
上面應該包含的值,但未定義當我console.log(scope.countObj)
這是指令:
ebApp.directive('monthDir', function() {
return {
restrict: 'E',
templateUrl: 'htmlFiles/monthDirective.html',
transclude: true,
scope: {
ebObj: "=obj",
onCl: "&",
countObj: "=" //this is undefined
},
link: function (scope, element, attrs) {
scope.change = function (changedUnit) {
changedUnit.totalCost = 0;
//console.log(changedUnit);
changedUnit.totalCost = changedUnit.unitsConsumed * 50;
console.log(scope);
console.log(scope.$eval(attrs.model));
}
scope.removeDir = function (removeVal) {
console.log(removeVal);
scope.onCl({ removeVal: attrs.model });
}
},
controller: function ($scope) {
//console.log($scope);
}
}
})
在控制器中,我有這$scope.countArray = [0];
,以便頁面加載時至少有一個指令。當我想新的指示我這樣做:
$scope.addDirective = function() {
$scope.countArray.push($scope.countArray.length);
}
新的價值應該被傳遞到指令的範圍,ng-repeat
。但它不會工作。我得到undefined
嘗試使用'範圍。$ parent' –
我認爲你需要使用'count-obj'作爲你的html中的attr,而不是'countObj'。你不需要使用數組索引'count-obj =「countArray [count]」',你可以使用'count-obj =「count」'。 –
@DanielWarke。你是對的。謝謝。寫爲答案 – Abhi