我想通过AngularJs Controller 在HTML中显示用户配置文件值,但是在绑定(bind)它们的html <p>
中没有显示。
jsfiddle
AngularJs应用程序:
var app = angular.module('myApp', []);
app.service('UserService', function () {
var userDetails = [{
id : 27,
first_name : 'Addy',
last_name : 'Villiams',
gender : 1,
email : 'addy@villiams.com',
creation_date : '2015-09-23 10:53:19.423',
age : 25,
profile_pic : 'avatar.get?uid=27'
}];
this.get = function () {
return userDetails;
}
});
app.controller('UserController', function ($scope, UserService) {
$scope.userinfo = UserService.get();
});
HTML:
<div ng-controller="UserController">
<p>{{userinfo.id}}</p>
<p>{{userinfo.first_name}}</p>
</div>
最佳答案
服务返回的对象是带有一个对象的数组。
$scope.userinfo = UserService.get()[0]; // Get the first element from array
Demo
或更改服务以返回对象而不是数组。
var userDetails = {
id: 27,
first_name: 'Addy',
last_name: 'Villiams',
gender: 1,
email: 'addy@villiams.com',
creation_date: '2015-09-23 10:53:19.423',
age: 25,
profile_pic: 'avatar.get?uid=27'
};
Demo