AngularJS $http response [duplicate]
This question already has an answer here:
How do I return the response from an asynchronous call?
33 answers
here is my code
var object = null
app.controller("controller",function($scope,service){
$scope.search=function(){
service.findData().then(
function successCallback(response){
object = response.data
},
function errorCallback(response){
alert("error")
}
)
console.log(object)
}
})
when I print the object, it is null so the question is: how can I get response.data outside?
Thank you for helping!
angularjs angularjs-scope angular-promise
marked as duplicate by georgeawg
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 22 '18 at 14:21
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
How do I return the response from an asynchronous call?
33 answers
here is my code
var object = null
app.controller("controller",function($scope,service){
$scope.search=function(){
service.findData().then(
function successCallback(response){
object = response.data
},
function errorCallback(response){
alert("error")
}
)
console.log(object)
}
})
when I print the object, it is null so the question is: how can I get response.data outside?
Thank you for helping!
angularjs angularjs-scope angular-promise
marked as duplicate by georgeawg
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 22 '18 at 14:21
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
2
writeconsole.log
inside your success callback. You are working with Promises, you can't get the data out. You can assign it to a scoped variable with$scope.object = ...
and then display it with{{object}}
for example
– Aleksey Solovey
Nov 22 '18 at 13:22
1
yep,i know $scope.object can work,but i want this object to do other things in controller ,so i want to use object out of findData()
– J.Smith
Nov 22 '18 at 13:28
1
then you have to keep your entire logic inside that success callback. Or you can return the Promise, but if you want any data from it, you would need to resolve it every time with.then
and still keep the logic inside the Promise
– Aleksey Solovey
Nov 22 '18 at 13:34
@AlekseySolovey I dont think doing the logic inside a sucesscallback is a good option. You can make a reference to the controller and write a controller function as done below in my answer.
– Jins Peter
Nov 22 '18 at 13:45
add a comment |
This question already has an answer here:
How do I return the response from an asynchronous call?
33 answers
here is my code
var object = null
app.controller("controller",function($scope,service){
$scope.search=function(){
service.findData().then(
function successCallback(response){
object = response.data
},
function errorCallback(response){
alert("error")
}
)
console.log(object)
}
})
when I print the object, it is null so the question is: how can I get response.data outside?
Thank you for helping!
angularjs angularjs-scope angular-promise
This question already has an answer here:
How do I return the response from an asynchronous call?
33 answers
here is my code
var object = null
app.controller("controller",function($scope,service){
$scope.search=function(){
service.findData().then(
function successCallback(response){
object = response.data
},
function errorCallback(response){
alert("error")
}
)
console.log(object)
}
})
when I print the object, it is null so the question is: how can I get response.data outside?
Thank you for helping!
This question already has an answer here:
How do I return the response from an asynchronous call?
33 answers
angularjs angularjs-scope angular-promise
angularjs angularjs-scope angular-promise
edited Nov 22 '18 at 15:13
the_ultimate_developer
1,006620
1,006620
asked Nov 22 '18 at 13:20
J.SmithJ.Smith
132
132
marked as duplicate by georgeawg
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 22 '18 at 14:21
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by georgeawg
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 22 '18 at 14:21
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
2
writeconsole.log
inside your success callback. You are working with Promises, you can't get the data out. You can assign it to a scoped variable with$scope.object = ...
and then display it with{{object}}
for example
– Aleksey Solovey
Nov 22 '18 at 13:22
1
yep,i know $scope.object can work,but i want this object to do other things in controller ,so i want to use object out of findData()
– J.Smith
Nov 22 '18 at 13:28
1
then you have to keep your entire logic inside that success callback. Or you can return the Promise, but if you want any data from it, you would need to resolve it every time with.then
and still keep the logic inside the Promise
– Aleksey Solovey
Nov 22 '18 at 13:34
@AlekseySolovey I dont think doing the logic inside a sucesscallback is a good option. You can make a reference to the controller and write a controller function as done below in my answer.
– Jins Peter
Nov 22 '18 at 13:45
add a comment |
2
writeconsole.log
inside your success callback. You are working with Promises, you can't get the data out. You can assign it to a scoped variable with$scope.object = ...
and then display it with{{object}}
for example
– Aleksey Solovey
Nov 22 '18 at 13:22
1
yep,i know $scope.object can work,but i want this object to do other things in controller ,so i want to use object out of findData()
– J.Smith
Nov 22 '18 at 13:28
1
then you have to keep your entire logic inside that success callback. Or you can return the Promise, but if you want any data from it, you would need to resolve it every time with.then
and still keep the logic inside the Promise
– Aleksey Solovey
Nov 22 '18 at 13:34
@AlekseySolovey I dont think doing the logic inside a sucesscallback is a good option. You can make a reference to the controller and write a controller function as done below in my answer.
– Jins Peter
Nov 22 '18 at 13:45
2
2
write
console.log
inside your success callback. You are working with Promises, you can't get the data out. You can assign it to a scoped variable with $scope.object = ...
and then display it with {{object}}
for example– Aleksey Solovey
Nov 22 '18 at 13:22
write
console.log
inside your success callback. You are working with Promises, you can't get the data out. You can assign it to a scoped variable with $scope.object = ...
and then display it with {{object}}
for example– Aleksey Solovey
Nov 22 '18 at 13:22
1
1
yep,i know $scope.object can work,but i want this object to do other things in controller ,so i want to use object out of findData()
– J.Smith
Nov 22 '18 at 13:28
yep,i know $scope.object can work,but i want this object to do other things in controller ,so i want to use object out of findData()
– J.Smith
Nov 22 '18 at 13:28
1
1
then you have to keep your entire logic inside that success callback. Or you can return the Promise, but if you want any data from it, you would need to resolve it every time with
.then
and still keep the logic inside the Promise– Aleksey Solovey
Nov 22 '18 at 13:34
then you have to keep your entire logic inside that success callback. Or you can return the Promise, but if you want any data from it, you would need to resolve it every time with
.then
and still keep the logic inside the Promise– Aleksey Solovey
Nov 22 '18 at 13:34
@AlekseySolovey I dont think doing the logic inside a sucesscallback is a good option. You can make a reference to the controller and write a controller function as done below in my answer.
– Jins Peter
Nov 22 '18 at 13:45
@AlekseySolovey I dont think doing the logic inside a sucesscallback is a good option. You can make a reference to the controller and write a controller function as done below in my answer.
– Jins Peter
Nov 22 '18 at 13:45
add a comment |
1 Answer
1
active
oldest
votes
It was a general practice during the era of AngularJS to assign the controller to variable.
app.controller("controller",function($scope,service){
var self = this;
$scope.search=function(){
service.findData().then(
function successCallback(response){
self.yourBusinessFunction (response.data)
},
function errorCallback(response){
alert("error")
}
)
console.log(object)
}
self.yourBusinessFunction = function(data){
console.log(data);
// data here will be the response.data from the service
// You can write your logic here
}
})
tks,i hava another question,if i hava 2 response with 2 search method ,how can i use one self.bussinessFunction to handle two response.data
– J.Smith
Nov 22 '18 at 14:06
sync both responses with$q.all([promise1, promise2]).then(function(response_array){...})
– Aleksey Solovey
Nov 22 '18 at 14:25
thx,i think i can solve it today :)
– J.Smith
Nov 23 '18 at 3:26
It is easy to work from the above basics ryt. On the success callback of the second service method, call the business functions. if you need another business function, write it as a property ofself
likeself.businessFunction2
in th same controller and use it likewise.
– Jins Peter
Nov 23 '18 at 5:12
i get it,this seems to be easier;
– J.Smith
Nov 23 '18 at 7:54
|
show 1 more comment
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
It was a general practice during the era of AngularJS to assign the controller to variable.
app.controller("controller",function($scope,service){
var self = this;
$scope.search=function(){
service.findData().then(
function successCallback(response){
self.yourBusinessFunction (response.data)
},
function errorCallback(response){
alert("error")
}
)
console.log(object)
}
self.yourBusinessFunction = function(data){
console.log(data);
// data here will be the response.data from the service
// You can write your logic here
}
})
tks,i hava another question,if i hava 2 response with 2 search method ,how can i use one self.bussinessFunction to handle two response.data
– J.Smith
Nov 22 '18 at 14:06
sync both responses with$q.all([promise1, promise2]).then(function(response_array){...})
– Aleksey Solovey
Nov 22 '18 at 14:25
thx,i think i can solve it today :)
– J.Smith
Nov 23 '18 at 3:26
It is easy to work from the above basics ryt. On the success callback of the second service method, call the business functions. if you need another business function, write it as a property ofself
likeself.businessFunction2
in th same controller and use it likewise.
– Jins Peter
Nov 23 '18 at 5:12
i get it,this seems to be easier;
– J.Smith
Nov 23 '18 at 7:54
|
show 1 more comment
It was a general practice during the era of AngularJS to assign the controller to variable.
app.controller("controller",function($scope,service){
var self = this;
$scope.search=function(){
service.findData().then(
function successCallback(response){
self.yourBusinessFunction (response.data)
},
function errorCallback(response){
alert("error")
}
)
console.log(object)
}
self.yourBusinessFunction = function(data){
console.log(data);
// data here will be the response.data from the service
// You can write your logic here
}
})
tks,i hava another question,if i hava 2 response with 2 search method ,how can i use one self.bussinessFunction to handle two response.data
– J.Smith
Nov 22 '18 at 14:06
sync both responses with$q.all([promise1, promise2]).then(function(response_array){...})
– Aleksey Solovey
Nov 22 '18 at 14:25
thx,i think i can solve it today :)
– J.Smith
Nov 23 '18 at 3:26
It is easy to work from the above basics ryt. On the success callback of the second service method, call the business functions. if you need another business function, write it as a property ofself
likeself.businessFunction2
in th same controller and use it likewise.
– Jins Peter
Nov 23 '18 at 5:12
i get it,this seems to be easier;
– J.Smith
Nov 23 '18 at 7:54
|
show 1 more comment
It was a general practice during the era of AngularJS to assign the controller to variable.
app.controller("controller",function($scope,service){
var self = this;
$scope.search=function(){
service.findData().then(
function successCallback(response){
self.yourBusinessFunction (response.data)
},
function errorCallback(response){
alert("error")
}
)
console.log(object)
}
self.yourBusinessFunction = function(data){
console.log(data);
// data here will be the response.data from the service
// You can write your logic here
}
})
It was a general practice during the era of AngularJS to assign the controller to variable.
app.controller("controller",function($scope,service){
var self = this;
$scope.search=function(){
service.findData().then(
function successCallback(response){
self.yourBusinessFunction (response.data)
},
function errorCallback(response){
alert("error")
}
)
console.log(object)
}
self.yourBusinessFunction = function(data){
console.log(data);
// data here will be the response.data from the service
// You can write your logic here
}
})
edited Nov 23 '18 at 6:29
answered Nov 22 '18 at 13:42


Jins PeterJins Peter
1,283520
1,283520
tks,i hava another question,if i hava 2 response with 2 search method ,how can i use one self.bussinessFunction to handle two response.data
– J.Smith
Nov 22 '18 at 14:06
sync both responses with$q.all([promise1, promise2]).then(function(response_array){...})
– Aleksey Solovey
Nov 22 '18 at 14:25
thx,i think i can solve it today :)
– J.Smith
Nov 23 '18 at 3:26
It is easy to work from the above basics ryt. On the success callback of the second service method, call the business functions. if you need another business function, write it as a property ofself
likeself.businessFunction2
in th same controller and use it likewise.
– Jins Peter
Nov 23 '18 at 5:12
i get it,this seems to be easier;
– J.Smith
Nov 23 '18 at 7:54
|
show 1 more comment
tks,i hava another question,if i hava 2 response with 2 search method ,how can i use one self.bussinessFunction to handle two response.data
– J.Smith
Nov 22 '18 at 14:06
sync both responses with$q.all([promise1, promise2]).then(function(response_array){...})
– Aleksey Solovey
Nov 22 '18 at 14:25
thx,i think i can solve it today :)
– J.Smith
Nov 23 '18 at 3:26
It is easy to work from the above basics ryt. On the success callback of the second service method, call the business functions. if you need another business function, write it as a property ofself
likeself.businessFunction2
in th same controller and use it likewise.
– Jins Peter
Nov 23 '18 at 5:12
i get it,this seems to be easier;
– J.Smith
Nov 23 '18 at 7:54
tks,i hava another question,if i hava 2 response with 2 search method ,how can i use one self.bussinessFunction to handle two response.data
– J.Smith
Nov 22 '18 at 14:06
tks,i hava another question,if i hava 2 response with 2 search method ,how can i use one self.bussinessFunction to handle two response.data
– J.Smith
Nov 22 '18 at 14:06
sync both responses with
$q.all([promise1, promise2]).then(function(response_array){...})
– Aleksey Solovey
Nov 22 '18 at 14:25
sync both responses with
$q.all([promise1, promise2]).then(function(response_array){...})
– Aleksey Solovey
Nov 22 '18 at 14:25
thx,i think i can solve it today :)
– J.Smith
Nov 23 '18 at 3:26
thx,i think i can solve it today :)
– J.Smith
Nov 23 '18 at 3:26
It is easy to work from the above basics ryt. On the success callback of the second service method, call the business functions. if you need another business function, write it as a property of
self
like self.businessFunction2
in th same controller and use it likewise.– Jins Peter
Nov 23 '18 at 5:12
It is easy to work from the above basics ryt. On the success callback of the second service method, call the business functions. if you need another business function, write it as a property of
self
like self.businessFunction2
in th same controller and use it likewise.– Jins Peter
Nov 23 '18 at 5:12
i get it,this seems to be easier;
– J.Smith
Nov 23 '18 at 7:54
i get it,this seems to be easier;
– J.Smith
Nov 23 '18 at 7:54
|
show 1 more comment
2
write
console.log
inside your success callback. You are working with Promises, you can't get the data out. You can assign it to a scoped variable with$scope.object = ...
and then display it with{{object}}
for example– Aleksey Solovey
Nov 22 '18 at 13:22
1
yep,i know $scope.object can work,but i want this object to do other things in controller ,so i want to use object out of findData()
– J.Smith
Nov 22 '18 at 13:28
1
then you have to keep your entire logic inside that success callback. Or you can return the Promise, but if you want any data from it, you would need to resolve it every time with
.then
and still keep the logic inside the Promise– Aleksey Solovey
Nov 22 '18 at 13:34
@AlekseySolovey I dont think doing the logic inside a sucesscallback is a good option. You can make a reference to the controller and write a controller function as done below in my answer.
– Jins Peter
Nov 22 '18 at 13:45