admin管理员组文章数量:1205743
I am using the follow code to get json object and bind it to the $scope
WORKING CODE:
$http({
url: '/Home/GetJson',
method: "GET",
params: {
clientID: cId
}
}).success(function (data) {
$scope.members = data.members;
})
It works .. what I would like to do is get the results into a var data
then add it to the $scope.
FAILING CODE :
var data = $http({
url: '/Home/GetJson',
method: "GET",
params: {
clientID: cId
}
}).success(function (data) {
return data.members;
})
$scope.members = data;
When I look at $scope.members, it is empty in the failing code because $scope.members is empty when it is filled (js is event based).
How can I wait till json returns > then var = data > then $scope.members = data ?
WORKING CODE
I used Javascript callback functions as below
Call my main function
DoLogin($scope, $http, email, password, AssingMemberToScope);
function DoLogin($scope, $http, email, password, callback) {
$http({
url: '/Home/GetJson',
method: "GET",
params: {
clientID: cId
}
}).success(function (data) {
callback($scope, data); //<----- Call Back occurs
})
}
//--Call-back Function working and code is separated --/
function AssingMemberToScope($scope, data) {
if (data.msg) {
$('.loading').hide();
$scope.member = data.member;
} else {
$('.loading').hide();
$('#msg').show();
}
}
I am using the follow code to get json object and bind it to the $scope
WORKING CODE:
$http({
url: '/Home/GetJson',
method: "GET",
params: {
clientID: cId
}
}).success(function (data) {
$scope.members = data.members;
})
It works .. what I would like to do is get the results into a var data
then add it to the $scope.
FAILING CODE :
var data = $http({
url: '/Home/GetJson',
method: "GET",
params: {
clientID: cId
}
}).success(function (data) {
return data.members;
})
$scope.members = data;
When I look at $scope.members, it is empty in the failing code because $scope.members is empty when it is filled (js is event based).
How can I wait till json returns > then var = data > then $scope.members = data ?
WORKING CODE
I used Javascript callback functions as below
Call my main function
DoLogin($scope, $http, email, password, AssingMemberToScope);
function DoLogin($scope, $http, email, password, callback) {
$http({
url: '/Home/GetJson',
method: "GET",
params: {
clientID: cId
}
}).success(function (data) {
callback($scope, data); //<----- Call Back occurs
})
}
//--Call-back Function working and code is separated --/
function AssingMemberToScope($scope, data) {
if (data.msg) {
$('.loading').hide();
$scope.member = data.member;
} else {
$('.loading').hide();
$('#msg').show();
}
}
Share
Improve this question
edited Aug 8, 2013 at 22:15
Ravi Ram
asked Jul 30, 2013 at 14:59
Ravi RamRavi Ram
24.5k21 gold badges86 silver badges119 bronze badges
2
- You're doing it correctly in the first sample by assigning it in the callback, why change it? – tymeJV Commented Jul 30, 2013 at 15:01
- I wanted to place the entire json call in a separate function and return the data into the function so I can do some stuff to it. – Ravi Ram Commented Jul 30, 2013 at 15:02
3 Answers
Reset to default 9Try this pattern:
angular.module('App').factory('service', function ($http) {
var service = {
myFunction: function (query) {
var promise = $http({
url: '/Home/GetJson',
method: "GET",
params: {
clientID: cId
}
}).success(function (data) {
return data.members;
});
return promise;
};
}
});
Then when consume the service, do
service.myFunction(query).then(function (data) {
$scope.members = data.members;
});
I think a better approach would be to put the JSON call into a function that accepts a callback
function as a parameter. Quick example:
function makeJSONCall(callback) {
$http({
url: '/Home/GetJson',
method: "GET",
params: { clientID: cId }
}).success(function (data) {
callback(data);
});
}
function someFunctionCallback(param) {
console.log(param)
}
Now, inside that callback
function, do what you want with the data. You can also call the JSON function when you need it now, a simple makeJSONCall(someFunctionCallback)
will do.
You actually explained it yourself in your last sentence, you can use promises and their then() callback
var data = $http({
url: '/Home/GetJson',
method: "GET",
params: { clientID: cId }
})
.then(function (data) {
return data.members;
})
};
...
...
data.then(function(response){
//play with the data
$scope.data=response
})
本文标签: javascriptReturn object from AngularJS http getStack Overflow
版权声明:本文标题:javascript - Return object from AngularJS $http get - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738693727a2107249.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论