[英]Angularjs view not displaying data returned from factory
I set up a service that consumes a rails api that is returning json. 我设置了一个服务,该服务使用了返回json的rails api。 The request comes back based on the console output as: 根据控制台输出,该请求将返回:
Resource {tests: Array[4], $promise: Promise, $resolved: true}
The problem I am having is nothing is displaying in my view. 我遇到的问题是我认为没有任何显示。 Do I need to convert the returned results somehow? 我是否需要以某种方式转换返回的结果? When I ping that route via postman I get a json object as my result set. 当我通过邮递员ping该路由时,我得到一个json对象作为我的结果集。
I have the following set-up: 我有以下设置:
/service/test.js /service/test.js
angular.module('myApp')
.factory('Test', function($resource) {
return $resource('http://api.myapp-dev.com:3000/tests/:id', { id: '@_id' },
{
'index': {
method:'GET',
headers: {
'Authorization':'<a token>'
},
isArray: false
}
});
});
/controllers/tests.js /controllers/tests.js
angular.module('myApp')
.controller('TestsCtrl', function ($scope, Test) {
$scope.tests = Test.index();
});
views/tests.html views / tests.html
<div>
<ul ng-repeat="test in tests">
<li>{{ test.value }}</li>
</ul>
</div>
You should do something like below in your controller. 您应该在控制器中执行以下操作。
Test.index(
function(response) {
$scope.tests = response.tests;
},
function(error) {
}
)
There are also other ways to do this like. 也有其他方式可以做到这一点。
Test.index().$promise.then(
function(response) {
$scope.tests = response.tests;
},
errorCallback
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.