angular js,为什么我的数据不可视化?

时间:2015-10-17 12:35:11

标签: angularjs scope ng-repeat

您好我正在尝试研究角度,但比我想象的要难。 我只想在控制器中可视化数组的某些值,但在浏览器中似乎显示任何内容。

这里是代码:

            <body ng-app="APP">
            <div ng-controller="theController">
                <b ng-repeat="item in items">{{item.title}}</b>
            </div>

            <script>
                    angular.module('APP',[])
                    .controller ('theController',['$scope',function($scope){
                    $scope.items[
                        {'title':'a','type':1},
                        {'title':'b','type':2},
                        {'title':'c','type':1},
                        {'title':'d','type':4}
                        ]
                }])
            </script>

            </body>
</html>

我只是复制一个现有的教程,为什么我可以在标签内看到任何内容?

2 个答案:

答案 0 :(得分:0)

只需更改

发件人

 $scope.items[
             {'title':'a','type':1},
             {'title':'b','type':2},
             {'title':'c','type':1},
             {'title':'d','type':4}
             ]

$scope.items = [
                {'title':'a','type':1},
                {'title':'b','type':2},
                {'title':'c','type':1},
                {'title':'d','type':4}
                ]

工作JsFiddle

答案 1 :(得分:0)

这是一个非常小的语法错误无忧无虑 - 确实非常容易和神奇!

&#13;
&#13;
 
angular.module('APP',[]) //app core module
               .controller ('theController',['$scope',function($scope){ //first controller to hold models
                $scope.items = [ //**Here = was missing; that was your minor mistake**//first model as array of objects
                    {'title':'a','type':1},
                    {'title':'b','type':2},
                    {'title':'c','type':1},
                    {'title':'d','type':4}
                    ]; //**semicolon was missing; putting is good practice**
            }]);//**semicolon was missing; putting is good practice**
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<!-- app init and letting div to be controlled by our controller named as theController -->
<div ng-app='APP' ng-controller='theController'>
  <ol > 
    <!-- angular magical iteration on model, on items in this case -->
    <li ng-repeat='item in items'>
      Index:{{$index}} ==> {{item.title}} {{item.type}}
    <li>    
  </ol>
</div>
&#13;
&#13;
&#13;

快乐帮助!