简体   繁体   中英

Pass a value by parameter and update it in the controller on click

Description: I have a collection of items. I show them into the view using ng-repeat, and then when I select any of the items, I want to set the 'currentState' value into true or false depending on some actions. Finally show it into the view.

Problem: When I pass 'currentState' to the controller, using ngClick, it's not being updated.

Code:

VIEW

 <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;
     }
});

The idea is use the same function ('exampleFunc') to update different independent values, for ex:

<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>

Is there any possibility to do something like this. Any help would be appreciate. Thanks very much.

use

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

see this example https://jsfiddle.net/jigardafda/ov3gftgs/1/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM