javascript - Angular JS access $http data from factory in controller -
javascript - Angular JS access $http data from factory in controller -
this question has reply here:
how homecoming response asynchronous call? 11 answersi starting out angular js , have created mill pupil data. pupil info looks below.
{ "male": [ ["john", "smith", "2000-10-10"], ["james", "smith", "2000-10-10"] ] }
i have mill , controller setup shown below
.factory('students', ['$http', function($http) { var info = {}; homecoming { getstudents: function () { $http.get('api/students.json'). success(function(data, status, headers, config) { console.log(data.male); //returns array of males data.studentlist = data.male; data.propertyone = 'propertyone'; //returns propertyone should homecoming in console.log(data) have farther downwards page. console.log(data.propertyone); }). error(function(data, status, headers, config) { // log error console.log('error'); }); data.propertytwo = 'propertytwo'; console.log(data); //returns propertytwo homecoming data; } } }]); .controller('studentctrl', ['$scope', 'students', function ($scope, students) { $scope.students = students.getstudents(); console.log($scope); // properytwo returned in students scope }])
what can not work out how info gets loaded on success added info object? can see comments, returning info in success no longer available outside of sucess function? need alter here working in controller can access pupil list returned students.json file.
this timing issue. when console.log(data); //returns propertytwo
runs still haven't made assignment in success callback yet. have move both success callback if timing critical here.
i recommend resolve promise in controller (add success callback there instead of doing in service.
javascript html angularjs
Comments
Post a Comment