通过参数传递值并在单击时在控制器中更新它

时间:2015-05-07 16:46:46

标签: javascript angularjs angularjs-ng-click

说明 我收集了一些物品。我使用ng-repeat将它们显示在视图中,然后当我选择任何项目时,我想设置' currentState'取决于某些操作,将值转换为true或false。最后将其显示在视图中。

问题: 当我通过' currentState'到控制器,使用ngClick,它没有被更新。

代码:

查看

 <md-select>
    <md-option ng-value="item.name" ng-click="exampleFunc(currentState)" ng-repeat="item in items>{{item.name}}</md-option>
 </md-select>
 <span>{{item.name}} - {{currentState}}</span>

的JavaScript

app.controller(...{
     $scope.items = [{name: ...}, {name: ...}];  //array of objects
     $scope.currentState = null;       //boolean var, null as default

     $scope.exampleFunc = function(aState) {           
        //some actions if true
         aState = true;
        //if false
         aState = false;
     }
});

这个想法是使用相同的函数(&#39; exampleFunc&#39;)来更新不同的独立值,例如:

<md-select>
    <md-option ng-value="item.name" ng-click="exampleFunc(anotherState)" ng-repeat="item in items>{{item.name}}</md-option>
 </md-select>
 <span>{{item.name}} - {{anotherState}}</span>

有没有可能做这样的事情。 任何帮助将是欣赏。 非常感谢。

1 个答案:

答案 0 :(得分:0)

使用

ng-click="exampleFunc($parent.currentState)"

请参阅此示例https://jsfiddle.net/jigardafda/ov3gftgs/1/