2014-01-22 51 views

回答

0

你JS语法不正确检索项目对象的名称。试试这个:

function appcontrol($scope){ 
    $scope.items = [{ 
     'name': 'shoe', 
     'color' : 'blue', 
     'price': '200', 
    }]; 
} 

请注意,我在一个阵列[]包裹items,和你错过=迹象。此外,非常重要的是,在jsFiddle上,角度将仅适用于无包裹 - 在<head>下设置框架&扩展。

请参阅更新的小提琴:http://jsfiddle.net/hzU7y/

编辑:这里是阵列与多个项目的例子:

$scope.items = [ 
    { 
     'name': 'shoe', 
     'color' : 'blue', 
     'price': '200', 
    }, 
    { 
     'name': 'shirt', 
     'color' : 'pink', 
     'price': '150', 
    }, 
    { 
     'name': 'shorts', 
     'color' : 'denim', 
     'price': '120', 
    } 
]; 

这可能是使用NG重复,因为它是重复的最自然的方式通过集合。

HTML:

<body ng-app> 
    <ul ng-controller='appcontrol'> 
     <li ng-repeat="(id, item) in items"> //<- note difference in the ng-repeat tag 
      {{ item.color }} {{ item.name }} at $ {{ item.price }} 
     </li> 
    </ul> 
</body> 

JS:

function appcontrol($scope){ 
$scope.items = { 
    'item1' : { 
     'name': 'shoe', 
     'color' : 'blue', 
     'price': '200', 
    }, 
    'item2' :{ 
     'name': 'shirt', 
     'color' : 'pink', 
     'price': '150', 
    }, 
    'item3' :{ 
     'name': 'shorts', 
     'color' : 'denim', 
     'price': '120', 
    }}; 
} 
+0

为什么使用对象不起作用,而需要使用数组呢? – user3189052

+0

好吧,我假设如果你想遍历'items',那么'items'应该是一个数组。 –

+0

是否可以像item = {item1:{},item2:{}}? – user3189052

0

我已经做了相同或者,你可以通过使用一个键值对做类似什么是您的评论中提到的东西SimonC但与其他清理和添加推荐NG-应用程序名称

HTML:

<body> 
    <div id='content' ng-app='MyTutorialApp' ng-controller='MainController'> 
    <ul ng-controller='appcontrol'> 
     <li ng-repeat="item in items">{{item.name}}</li> 
    </ul> 
    </div> 
</body> 

和JS:

var app = angular.module('MyApp',[]); 

app.controller("AppController", function($scope){ 
    $scope.items{ 
     'name': 'shoe', 
     'color' : 'blue', 
     'price': '200'; 
    } 
}); 

http://jsfiddle.net/yd4VS/4/