在我的一个工厂中,我需要在获取数据时设置一个变量(通过 $http
),这样我就可以在我的 Controller 中访问它(目的是显示一个微调图像直到数据已加载)。
.factory('LoadData', function LoadData($http, $q){
return {
getMyData: function(){
return $http(
// implementation of the call
).then(
function(response){
var myData = response.data;
// this should be reference to the other method (getLoadStatus)
// where I want to change its value to "true"
// this doesn't work - "this" or "self" don't work either because we're in another function
LoadData.getLoadStatus.status = true;
}
);
},
// by calling the below method from my controller,
// I want to mark the completion of data fetching
getLoadStatus = function(){
var status = null;
return status;
}
}
}
我希望您明白了 - 如何实现?此外,我愿意接受任何旨在改进方法的建议(我想尽可能采用最佳实践)。
最佳答案
Status本质上是一个私有(private)变量;用作:
.factory('LoadData', function LoadData($http, $q){
var status = null; // ESSENTIALLY PRIVATE TO THE SERVICE
return {
getMyData: function(){
return $http(...).then(function(response){
...
status = true;
});
},
getLoadStatus = function(){
return status;
}
};
})
关于javascript - Factory 有两种方法,一种是 $http 方法和 $promise - 如何从第一个方法的成功函数中引用另一种方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26223114/