2016-02-16 54 views
-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; 
     } 
    } 

]); 

回答

0

不要在ng-click使用{{...}}。这就够了:

ng-click="setValue(keyXYZ)"