我想获得我从服务中返回的数据的特定属性。
所以基本上我希望能够以某种方式达到$ scope.users.name,我知道用户是数组中的对象,但是我能以任何方式访问该特定属性吗?希望这个问题足够清楚?
$scope.users = [];
UserService.getAll().then(
function (data) {
$scope.users = data;
}, function (err) {
console.log(err);
}
);
答案 0 :(得分:2)
我假设您收到的数据是数组形式。如果您知道索引,那么您可以
$scope.users[2].name
其中2是您想要知道名称属性的对象的索引。
或者你可以为每个
尝试一个js函数$scope.users.forEach(function (user) {
console.log(user.name);
});
该函数将遍历所有对象,您可以在传入的回调中访问它们的属性。
希望这是你正在寻找的东西。