2

我的html代码;

            <!doctype html>
            <html ng-app ng-controller="peopleController">
              <head>
                <script src="js/angular.min.js" type="text/javascript"></script>
                <script>
                function peopleController($scope){
                        $scope.people = [
                            { name : "aa" , age : 14 },
                            { name : "ss" , age : 11},
                            { name : "dd" , age : 12},
                            { name : "ff" , age : 16},
                            { name : "gg" , age : 13}
                        ];

                        $scope.keys=(function(obj){
                            var keys = [];
                            for(var key in obj){
                                console.log(obj);
                                if(obj.hasOwnProperty(key)){ keys.push(key);}
                            }
                            return keys;
                        })($scope.people[0]);
                    }
                </script>
                <title>aasss</title>
              </head>
              <body >
              <select ng-model="aaa">
                <option ng-repeat="x in keys" value="{{x}}">{{x}}</option>
              </select> 
              </body>
            </html>

为选择元素生成 html 部分;

            <select ng-model="aaa" class="ng-pristine ng-valid">
                <option value="? undefined:undefined ?"></option>
                <!-- ngRepeat: x in keys -->
                <option ng-repeat="x in keys" value="name" class="ng-scope ng-binding">name</option>
                <option ng-repeat="x in keys" value="age" class="ng-scope ng-binding">age</option>
            </select>

当我从选择元素中删除 ng-model="aaa" 属性时,问题不会出现。可能是什么问题呢?

4

1 回答 1

4

您不应该在 select 中使用 ng-repeat,review select doc。select 有自己的理解机制。另外,不确定为什么选择器中有 2 ng-repeat。似乎您可能想要封装 2 组键。

于 2012-11-01T17:30:19.323 回答