-2
我試圖將父控制器屬性作爲參數傳遞給子控制器函數,但它不起作用,我是新角度...作爲setValue中的{{keyXYZ}}模板值)未正確更換,但正確的值被映射在getIamgeState功能在下面的代碼...將父控制器值傳遞給子控制器函數
<div ng-controller="ParentCtrl">
{{keyXYZ}}
<p> one Key is = {{xyzFunction()}}</p>
<div ng-controller="ChildCtrl">
<button ng-click="setValue('{{keyXYZ}}')" ng-class="imageState = getImageState('{{keyXYZ}}')"> set a Value</button>
</div>
</div>
angular.module('myApp', [])
.controller('ParentCtrl', ['$scope', function ($scope) {
$scope.keyABC = "somekey1";
$scope.keyXYZ = "somekey2";
$scope.keyLALA = "somekey3";
$scope.abcFunction = function() {
return true;
}
$scope.xyzFunction = function() {
return $scope.keyXYZ;
}
}]).controller('ChildCtrl', ['$scope', function ($scope) {
$scope.childKey = 'initial Value';
$scope.setValue = function (val) {
$scope.childKey = 'new Key is '+ val;
}
$scope.getImageState = function (val) {
if (val == 'somekey2')
return true;
}
}
]);