I have created a small project, but I have some problems. I want to run some JavaScript code after my query. My code is like this:
Book.get({
id: $stateParams.id,
min: 0,
max: 3
}).$promise.then(function(data){
vm.list=data;
}).finally(function () {
});
vm.top=vm.list.name;
vm.top
is undefined
, because the query has not returned yet.
This is not how you should deal with asynchronous calls. The reason being is (in short) a JavaScript engine executes synchronous code first and then it executes asynchronous code.
Ideally you should wait until they accomplish and put your desired work inside their callback function. So over here you can either
.then
of Book.get()
function // 1. First way
var getBook = Book.get({id:$stateParams.id,min:0,max:3}).$promise.then(function(data){
vm.list=data;
// Put your code here
}).finally(function () {
});
// 2. Second way
getBook.then(function(){
vm.top= vm.list && vm.list.name;
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.