javascript - How to get json data in my case? -
I'm trying to use httml to get JSMS
I have something like that
.controller ('testcontroller', function ($ scope, $ state, $ http) {$ http ({url: "/json/test.json", Method: "GET" ,}). Success (data) {$ scope.testData = data; console.log (data)}; // $ outside the scope of http, but under the same controller, I have // shows it undefined Console.log ($ scope .testData)})
I know that we have to keep our login code so that I can get data inside $ http Do not want to modify it. Is there any way to accomplish it? Thank you!
First of all, Do not put a vote in the controller to take it into a service. This is the service that is designed for.
Second, the $ http service is asynchronous. Your result is only available in success callback, and by the completion of the http request Your console.log will first be called. You can use it to use outside of the callback. Scope is required to assign the variables, and the call is expected to clear until the backup is complete.
provides an example of an AngularJS authentication service, if you go with your example:
.controller ('testcontroller', function ($ radius, $ State, $ http) {$ scope.testData = {}; $ Http ({url: "/json/test.json", method: "GET",}). Success (work) {$ scope.testData = Data; $ scope.logData ();}); $ Scope.logData = function () {console.log ($ scope.testData);}}}
Comments
Post a Comment