javascript - JSONP Query and AngularJS -
i trying iterate through array of usernames , add resulting jsonp requests array can displayed using angularjs. follows:
html section
<div id = "displayul" ng-controller="usercontroller"> <ul> <li ng-repeat="user in results">{{user.user}}<img ng-src="{{user.logo}}">{{user.etcetera}}</li> </ul> </div>
javascript section
var app = angular.module('userapi', []); app.controller('usercontroller', function($scope,$http){ //our user name array $scope.inputusers= ["usera", "userb", "userc"]; $scope.results = []; //loop through each user $.each($scope.inputusers,function(key,value){ var currentuserdata = {}; currentuserdata.user = value; //url request - defined elsewhere callback jsonp var currenturl = streamurl + value + callbackpostfix; $.getjson(currenturl, function(data){ //update temp obj current data results currentuserdata.found = data.found currentuserdata.logo = data.logo; ... currentuserdata.ecetera = data.ecetera; //update results array current data obj $scope.results.push(currentuserdata); }); });
when runs, results array empty. jsonp callback fire before angular has chance update?
assuming url , response correct, problem using $.getjson
isn't part of angular.
whenever make changes scope code outside of angular core need tell angular run digest update view scope changes using $scope.$apply()
.
i suggest convert using $http.jsonp
instead handle digests internally.
$.getjson(currenturl, function(data){ //update temp obj current data results currentuserdata.found = data.found currentuserdata.logo = data.logo; ... currentuserdata.ecetera = data.ecetera; //update results array current data obj $scope.results.push(currentuserdata); // time digest update view $scope.$apply(); });
Comments
Post a Comment