目标: – 仅显示单个对象数据.
我是否必须使用ng-repeat来获取对象?
我是否必须使用ng-repeat来获取对象?
我对角度相对较新.有一个更好的方法吗?
Html查看: –
<div ng-controller="dashboardController"> <div ng-repeat="person in persons"> <span class="name">{{person.name}}</span> <span class="title">{{person.title}}</span> </div> </div>
控制器代码: –
.controller('dashboardController',['$scope',function(scope){ scope.persons = [ { name:"George Harrison",title:"Goof Off extraordinaire" } ]; }])
更新我的FELLOW NOOBS,数组与单数据集:
scope.persons = [ <-- that creates an array. Cant believe I forgot that. scope.persons = { <-- that creates a single data set
解决方法
scope.persons是一个数组,所以你必须使用ng-repeat.
如果您的数据是对象,则不需要使用ng-repeat.
例如:
你的控制器
如果您的数据是对象,则不需要使用ng-repeat.
例如:
你的控制器
.controller('dashboardController',function(scope){ scope.person ={ name:"George Harrison",title:"Goof Off extraordinaire" } }]);
所以你的HTML:
<div ng-controller="dashboardController"> <div> <span class="name">{{person.name}}</span> <span class="title">{{person.title}}</span> </div>