在 angular.forEach 中无法访问数组
Array is not accessible in angular.forEach
在工厂外,我使用 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());
但我得到的只是 []
谢谢。
改变
$scope.contractor = Contractors.get({id: $routeParams.coid });
到
Contractors.get({id: $routeParams.coid }).then(function(response) {
$scope.contractor = response;
});
因为 get
returns 一个 Promise 对象,并且您从 then
回调中获得了已解析的值。
在工厂外,我使用 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());
但我得到的只是 []
谢谢。
改变
$scope.contractor = Contractors.get({id: $routeParams.coid });
到
Contractors.get({id: $routeParams.coid }).then(function(response) {
$scope.contractor = response;
});
因为 get
returns 一个 Promise 对象,并且您从 then
回调中获得了已解析的值。