[英]AngularJS : Getting data from Factory and update Controller scope and view
大家好,我確實需要有關此工廠和控制器問題的幫助和建議。
我有一家從服務器獲取數據的工廠
sp.factory('homeFeed',['$window','$http','auth',function($window,$http,auth){ var url = auth.url; var version = auth.version; var HomeFeed = {}; HomeFeed.getFeeds = function(user){ //setting variable for get request on home feed var req = { method: 'GET', url: url + version + '/Feed', headers: { 'Content-Type': 'application/json; charset=utf-8', 'Authorization': 'Bearer ' + token }, } return $http(req).success(function(res){ return res; }); }; return HomeFeed; }]);
controller--
sp.controller('HomeCtrl',['$scope','homeFeed','$window',function($scope,homeFeed,$window){ //getting all the home feed data $scope.feeds = homeFeed.getFeeds(JSON.parse($window.localStorage['SP-User'])) }]);
但是,在服務器響應之后,我的視圖未更新,$ scope.feeds也未更新。 非常感謝您的幫助
當您進行異步$http
調用時,該數據在該時間實例將不可用。 當ajax調用成功時,它將可用。 您需要使用.then
函數,該函數將創建一個.success
鏈,並在.success
函數返回數據時執行一個函數。
調節器
sp.controller('HomeCtrl',['$scope','homeFeed','$window',
function($scope,homeFeed,$window){
//getting all the home feed data
homeFeed.getFeeds(JSON.parse($window.localStorage['SP-User']))
.then(function(data){
$scope.feeds = data
});
}
]);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.