2015-10-17 26 views
1

你好,我试图研究角度,但比我想象的更难。 我只想要在控制器内形象化一个数组的值,但在浏览器中似乎显示任何东西。角js,为什么我的数据不可视化?

这里的代码:

  <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> 

我只是复制现有的教程,所以为什么我可以看到,在标签内什么?

回答

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

+0

:啊哈哈很简单 –

0

它是一个非常小的语法错误没有忧虑 --Angular是非常容易和神奇的确!


 
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**
<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>

快乐帮助!