Javascript 如何在Angular中处理JSON响应

Javascript 如何在Angular中处理JSON响应,javascript,json,angularjs,Javascript,Json,Angularjs,我有以下控制器: function EntryCtrl($scope, Restangular){ console.log("*** EntryCtrl"); $scope.roles= Restangular.all("roles").getList(); console.log("*** EntryCtrl: " + JSON.stringify($scope.roles)); } 控制台输出为: *** EntryCtrl: {"restangularCollection":tr

我有以下控制器:

function EntryCtrl($scope, Restangular){
 console.log("*** EntryCtrl");
 $scope.roles= Restangular.all("roles").getList();
 console.log("*** EntryCtrl: " +  JSON.stringify($scope.roles));
}
控制台输出为:

*** EntryCtrl: {"restangularCollection":true} 
当我使用rest浏览器插件进行相同的rest调用时,我得到:

[
 {
     "name": "dev"
 }
]
我不知道如何处理与rest浏览器插件相同的响应。 我的最终目标是在Angular中获得角色名dev。 在有角度的页面上,我想在使用角色名称的链接上使用ng show,如下所示:

<a href="" ng-show="roles.contains('')>Foo</a>
试一试

$scope.roles= Restangular.all("roles").getList().$object;

a是否可以访问/访问帐户 默认情况下返回空数组。一旦从服务器返回值 该数组将填充这些值。所以你可以在模板中使用它

根据该文件:

restanglar使用承诺。而不是像$resource这样神奇地填充对象

因此,要从服务器获取响应,您必须使用。然后像这样回调:

Restangular.all("roles").getList().then(function(response){
    $scope.roles = response;
});
在模板中:

<li ng-repeat="role in roles">{{role.name}}</li>
<li ng-repeat="role in roles">{{role.name}}</li>