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

断了今生、忘了曾经 提交于 2019-12-01 09:04:45
Ajay Beniwal

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

                });
            };
        });
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!