【发布时间】:2014-10-22 01:39:30
【问题描述】:
亲爱的 AngularJS 专家,
如何在具有工厂返回承诺的控制器中设置 $scope.data。我所能做的就是使用工厂对象的方法范围内返回的数据,并且我不能将数据“加载”到控制器范围内现有的值中。如何将数据传递到控制器的范围内?
代码序列如下 这是工厂:
var features = {};
// the method returns a promise tot he controller needed to be delt with .then()
features.getPromise = function(){
//make a promise
var promise = $http({method: 'GET', url: '../../LIBS/inc/getAllGeoFeatures.php'})
.success(function(data, status, headers, config){
return data;
})
.error(function(data, status, headers, config){
$log.warn(data, status, headers, config);
});
return promise;
};
这是控制器:
$scope.data = 'null';
factGetFeat.getPromise().then(function(promise){
$scope.data = promise;
$log.info($scope.data); //gets the object all right
// here I can work with the data but I cannot share it with the controller
// i.e. with the view
});
$log.info($scope.data); //$scope.data is still null and I need it loaded with the data
我需要一些建议,因为我觉得我无处可去
我什至尝试用方法的结果加载 $scope.data,但我只得到了承诺对象而不是数据:
对象{then:函数,catch:函数,finally:函数}
请指教。
附:我使用角度 1.2.x
非常感谢您的时间和耐心。
【问题讨论】:
标签: javascript angularjs http promise