2015-10-17 21 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>

快樂幫助!