Getting data from a web service with Angular.js

前端 未结 4 1730
孤城傲影
孤城傲影 2020-12-29 00:13

Im trying to get data in a Json format from a remote WS using Angular and im having some trouble. The data comes from the web service correctly but i cant use it inside the

4条回答
  •  灰色年华
    2020-12-29 00:51

    Create the bookJSON as array, and push the elements instead of assignment. So

    var bookJSON=[];

    Inside $http.get do

    data.forEach(function(item) { bookJSON.push(item); });

    The second console log will show undefined because, the call is async. The assignment happens in future.

    The run method does not guarantee, that the code is run before controller loads.

    There are other ways too to solve this issue.

    Avoid global variable. Look at $routeProvider resolve property.

    Or implement a service to get this data as promise.

提交回复
热议问题