努力将结果从工厂返回到范围
Struggling to return result to scope from factory
我正在尝试从工厂返回过滤后的对象。在"return getTheChosen"点,对象符合预期。我无法将其分配给我的$scope.postDetail!
任何建议表示赞赏!
app.factory('postsFactory', function ($http) {
var response = $http.get('http://myjsonendpoint.com/');
var factory = {};
factory.list = function () {
return response;
};
factory.get = function (id) {
var getTheChosen = factory.list().then(function (res) {
var chosen = _.find(res.data, {'id': id});
return chosen;
});
return getTheChosen;
};
return factory;
});
然后。。。
app.controller('ThoughtsController', function ($scope, postsFactory) {
postsFactory.list()
.then(function (data) {
$scope.posts = data;
});
});
然后。。。
app.controller('PostDetailController', function ($scope, postsFactory, $routeParams) {
$scope.postDetail = postsFactory.get(parseInt($routeParams.postId));
$scope.test = 'yep';
});
在PostDetailController中以另一种方式执行此操作:
postsFactory.get(parseInt($routeParams.postId)).then(function(data) {
$scope.postDetail = data;
});
而不是:
$scope.postDetail = postsFactory.get(parseInt($routeParams.postId));
希望这会起作用
相关文章:
- 如何将闭包内的值返回到外部范围
- 范围变量返回长度错误
- 如何在 Google 应用脚本中 sendEmail 函数的范围内的唯一非空白列中返回值
- 返回Javascript中给定正则表达式的文本范围数组
- Angular$http返回数据,但不返回'不适用于范围
- d3范围/域返回负值
- 在全局范围内执行文件,而不是返回导出的对象
- 我从闭包返回的函数在外部范围内是否可用
- 我的弹性查询中出错 - 范围返回错误的请求
- 从显示为文本的范围中返回最小和最大数字
- 努力将结果从工厂返回到范围
- 在 Angular JS 中使用服务无法返回范围值
- 如何让此函数从范围中添加或减去并返回它
- 主干.js Model.get() 返回 undefined, 范围使用 coffeescript + coffee t
- Angular js 指令控制器访问范围属性,但返回未定义
- AngularJS 指令范围和$compile - 返回空白的变量
- Javascript 范围:为什么 foo 返回未定义
- JavaScript 函数返回范围
- 一起使用 .map(.range(1)) 的目的是什么?范围始终返回 [0]
- 在Google Apps Script中返回“范围”而不是实际字符串