【发布时间】:2015-05-03 12:06:43
【问题描述】:
在工厂外,我使用 express 和 MongoDB 获取数据,并在模型上定义了 mongoose,但我可以通过 id 获取文档并将其放在范围内,但我需要遍历人员数组,它适用于ng-repeater 在视图上,但我想选择一个子文档或数组 $scope.contractor.crews 将其从控制器发送到视图,因为下面的输出显示它在 $scope 上。
这是使用 $resource 的服务
.factory('Contractors', ['$resource', function($resource){
return $resource('/contractors/:id', null, {
'update': { method:'PUT' }
});
}])
这是我给工厂的电话。 $scope.contractor = Contractors.get({id: $routeParams.coid });
contractor: f
$promise: Object
$resolved: true
__v: 0
_id: "553569cbbb73900f82b8359a"
active: true
address: "In Delano too"
commission: 30
crews: Array[4]
0: Object
1: Object
2: null
3: null
length: 4
__proto__: Array[0]
fax: "6612953423"
name: "Magbar Contracting"
note: "This one has Mayra maya"
phone: "6613458584"
updated_at: "2015-04-20T21:04:11.681Z"
我试过了
$scope.crews = function() {
var crews = [];
angular.forEach($scope.contractor.crews, function(crew) {
crews.push(crew)
});
return crews;
};
console.log($scope.crews());
但我得到的只是[]
谢谢。
【问题讨论】:
-
抱歉错字已修复在帖子上代码正确但不起作用
标签: angularjs mongodb mongoose