为什么没有从函数返回该值
Why is the value not returned from the function?
我在角度服务中有以下代码:
factory.getList = function(){
$http.get('/getClasses')
.success(function (response) {
console.log(response);
return(response);
})
.error(function() {
console.log('error in getList');
});
};
当我运行它时,我可以在控制台中看到响应,但在控制器中我有:
$scope.classesList = Svc.getList();
并且此类列表始终是未定义的。为什么?
它是一个
异步回调函数,你需要做一些事情
首先将调用 B 函数传递给服务
factory.getList = function(callb){
$http.get('/getClasses')
.success(function (response) {
console.log(response);
callb(response);
})
.error(function() {
console.log('error in getList');
});
};
然后在控制器中这样调用它
Svc.getList(function(data){
$scope.classesList=data;
});
相关文章:
- 使用返回函数sinde.attr()jquery元素
- 从自执行函数返回函数的Javascript性能命中率
- Node Express Handlebars帮助程序未返回函数的结果
- 未在Firefox中执行PageMethod的返回函数
- 对返回函数的函数感到困惑
- 从承诺返回不返回函数会导致警告
- 从函数返回函数而不调用返回的函数
- Javascript,闭包中的返回函数如何与外部函数连接
- 为什么Coderbyte.com's的Javascript模板喜欢返回函数的原始参数
- 对象函数返回函数而不是值
- 从外部函数(数组)了解返回函数(x)
- 从Javascript类对象返回函数
- 调用Typescript setter don't返回函数,尽管关联的getter可以工作
- 使用依赖注入在 JavaScript 中返回函数
- 通过单击JSP和javascript加载两个返回函数
- 需要说明:无法理解返回函数的javascript
- 简单的onClick返回函数不起作用
- 为什么这个闭包返回函数
- JS函数返回函数供以后使用-未定义参数
- Coffeescription类中的方法返回函数而不是字符串