当我这样定义AngularJS工厂返回“未定义”
dataFactory.all = function() {
return 'test01';
}
工厂方法并调用它在控制器这样
console.log(Data.all());
我可以得到test01
打印。然而,当我在工厂添加一些逻辑all()
方法这样
dataFactory.all = function() {
$http
.get('/api/hey')
.success(function(data) {
$http
.get('/api/hi')
.success(function(data) {
return 'test'; // 'test' can not be printed, and console show 'undefined'
});
});
//return 'test01'; //can be printed;
};
那么“测试”不能经由控制器打印。因为我在回调中放入return
声明?
请让我知道我在做什么错了?
谢谢。
谢谢@pultrone – leonsPAPA
没有必要使用'$ q.defer()' 只需返回'$ http.get()'的结果使用$ http.get()的地方)足以让这个函数起作用。 $ q应该将承诺链接在一起。 – HankScorpio
是的,有你,我认为@ HankScorpio – leonsPAPA