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();
}
}
HttpClient.get() returns response datalink HttpClient.get() returns the body of the response as an untyped JSON object by default.
$http is an AngularJS service for reading data from remote servers.
$stateProvider is used to define different states of one route. You can give the state a name, different controller, different view without having to use a direct href to a route. There are different methods that use the concept of $stateprovider in AngularJS.
Use the HttpClient.get() method to fetch data from a server. The asynchronous method sends an HTTP request, and returns an Observable that emits the requested data when the response is received. The return type varies based on the observe and responseType values that you pass to the call.
Try 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
})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With