How to update ng-model dynamically in ng-repeat?

前端 未结 1 934
刺人心
刺人心 2021-01-14 09:13

I am facing some problem with dynamic ng-model values in my angular page. Here is my sample JSON.

mytabs = [
    {
        name : \"tab1\",
        values :          


        
相关标签:
1条回答
  • 2021-01-14 09:40

    You should modify you code a little bit you should add a checked property in the object and bind checkbox to that model .

    Kindly could use the below idea or code to get what you want more closely

     <div ng-repeat="tab in mytabs">
      <h1>{{tab.name}}</h1>
        <div ng-repeat="val in tab.values">
            <input type="checkbox" ng-model="val.checked"/>
        </div>
    </div>
    <input type="button" ng-click="checkValues()" value="checkitems" />
    
        <script>
            var app = angular.module('plunker', []);
    
            app.controller('MainCtrl', function ($scope,$filter) {
                $scope.mytabs = [
                    {
                        name: "tab1",
                        values: [
                            { value: "value1",checked:false },
                            { value: "value2", checked: false },
                            { value: "value3", checked: false },
                            { value: "value4", checked: false }
                        ]
                    },
                    {
                        name: "tab2",
                        values: [
                           { value: "value1", checked: false },
                           { value: "value2", checked: false },
                           { value: "value3", checked: false },
                           { value: "value4", checked: false }
                       ]
                    }
                ];
    
                $scope.checkValues = function () {
                    angular.forEach($scope.mytabs, function (value, index) {
                        var selectedItems = $filter('filter')(value.values, { checked: true });
                        angular.forEach(selectedItems, function (value, index) {
                            alert(value.value);
                        });
    
                    });
                };
            });
    
    0 讨论(0)
提交回复
热议问题