在ngRepeat

时间:2016-02-05 20:02:46

标签: javascript angularjs angularjs-ng-repeat angular-ngmodel

如何在ng-repeat中设置动态创建的ng模型的值?

我尝试将每个动态ng模型设置为truefalse

html文件

<md-list-item ng-repeat="awayTeamPlayer in ulineup.awayTeamPlayersLineup ">
    <md-checkbox ng-model="ulineup.awayTeamPlayersPlayed[awayTeamPlayer.player]"></md-checkbox>
    {{awayTeamPlayer.player}}
</md-list-item>

控制器文件

(function() {

    angular
        .module('app')
        .controller('UpdateLineUpController',
                    ['$routeParams', '$firebaseArray', 'FIREBASE_URL',
                    function($routeParams, $firebaseArray, FIREBASE_URL) {

            var vm = this;
            var gameId = $routeParams.gameId;
            var lineupId = $routeParams.lineupId;
            var awayTeamPlayersLineupRef = new Firebase(FIREBASE_URL + 'games/' + gameId + '/lineup/' + lineupId + '/awayTeamPlayersLineup');
            vm.awayTeamPlayersLineup = $firebaseArray(awayTeamPlayersLineupRef);

            // tried the following but that doesn't work
            angular.forEach(vm.awayTeamPlayersLineup, function(value, key) {
                if(value.played === true) {
                    // trying to set ng-model value to true gives me the following error
                    // TypeError: Cannot set property 'Mark' of undefined
                    vm.awayTeamPlayersPlayed[value.player] = true;
                } else {
                    vm.awayTeamPlayersPlayed[value.player] = false;
                }
            });

        }]);

})();

1 个答案:

答案 0 :(得分:0)

这里尝试使用init在内存中创建属性。

<md-list-item ng-repeat="awayTeamPlayer in ulineup.awayTeamPlayersLineup ">
<md-checkbox ng-init="dynamicProp = 'whatever'" ng-model="ulineup.awayTeamPlayersPlayed[awayTeamPlayer.player]"></md-checkbox>
{{awayTeamPlayer.player}}
</md-list-item>