[英]Angular SPA not showing API data on state activation
I have an Angular.js Single Page Application app (Ionic Framework) communicating with the Rails backend. 我有一个与Rails后端通信的Angular.js单页应用程序应用程序(离子框架)。
I use a $http
service to get data from my backend API. 我使用
$http
服务从后端API获取数据。
I'm facing a problem which demontrates itself by sometimes not showing the data fetched from backend. 我面临的一个问题是,有时不显示从后端获取的数据,这会使其本身变得不清晰。 I start the app, the
$http.get
call is issued and data is returned (I can see it in logs) but it's not shown. 我启动应用程序,发出
$http.get
调用并返回数据(我可以在日志中看到它),但未显示。 So, upon calling the data app activates the state which should show the data returned (in html template through ng-repeat…), and it's empty. 因此,在调用数据应用程序时,会激活状态,该状态应显示返回的数据(通过ng-repeat在html模板中…),并且为空。
If I then change the app state and return to the previous state, the data is shown. 如果然后更改应用程序状态并返回到先前的状态,则会显示数据。 When I use Angular's $resource service (like here: http://www.sitepoint.com/creating-crud-app-minutes-angulars-resource/ ) I get data fetched and showed but I run into some problems later and therefore cannot use
$resource
. 当我使用Angular的$ resource服务时(例如: http : //www.sitepoint.com/creating-crud-app-minutes-angulars-resource/ ),我获取并显示了数据,但是后来遇到了一些问题,因此无法解决使用
$resource
。
I suppose the template is empty because it is shown before the data is actually returned from API. 我认为模板是空的,因为它是在实际从API返回数据之前显示的。 How can I fix this problem?
我该如何解决这个问题? I've tried everything I could think of, all flavours of $http.get, .success() or .then(), promises, even
$timeout
... Below are the code snippets from actual app: 我用尽了一切我能想到的,)的$ http.get,.success所有香精 (或。那么(),承诺,甚至
$timeout
......下面是实际的应用程序中的代码片段:
controllers.js: controllers.js:
.controller('EnemiesCtrl', function($scope, $stateParams, Enemies ) {
$scope.enemies = Enemies.all(window.localStorage['id']);
})
services.js: services.js:
.factory('Enemies', ['$http', function($http, userId) {
// Use a resource here that returns a JSON array
var enemies = null;
userId = window.localStorage['id'];
$http({
url:"http://localhost:3000/home/api",
method: "GET",
params: {user_id: userId}
}).then(function(resp){
enemies = resp.data;
console.log('Success', resp.data);
},function(err) {
console.error('ERR', err.status);
alert('Error: '+err.status);
// err.status will contain the status code
});
return {
all: function() {
return enemies;
}
}
}]);
template.html: template.html:
<ion-item ng-repeat="enemy in enemies" type="item-text-wrap" href="#/tab/place/{{enemy.id}}">
enemy.name</br>
</ion-item>
Place your $http call in the all() function. 将$ http调用放在all()函数中。 The http call is async.
http调用是异步的。 The enemies var is returned when it´s null.
如果为null,则返回敌人var。 After the call to your API is made, the enemies var is your data.
调用您的API后,敌人var是您的数据。 So now try to execute the http call AFTER it´s finished (then).
因此,现在尝试完成http调用,然后再执行。 Then return the var enemies.
然后返回无敌。
Try: 尝试:
return {
all: function() {
$http({
url:"http://localhost:3000/home/api",
method: "GET",
params: {user_id: userId}
}).then(function(resp){
var enemies = resp.data;
return enemies;
},function(err) {
console.error('ERR', err.status);
alert('Error: '+err.status);
// err.status will contain the status code
return(err.status);
});
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.