Javascript angular js-无法访问控制器中推送到阵列的对象
这是我的控制器Javascript angular js-无法访问控制器中推送到阵列的对象,javascript,angularjs,angularjs-scope,angularjs-ng-repeat,Javascript,Angularjs,Angularjs Scope,Angularjs Ng Repeat,这是我的控制器 angular.module("app").controller('myController', ['$scope', '$filter','$rootScope','contentService','$location','$anchorScroll', function ($scope, $filter,$rootScope,contentService,$location,$anchorScroll) { $scope.searchContents = [] ; var
angular.module("app").controller('myController', ['$scope', '$filter','$rootScope','contentService','$location','$anchorScroll', function ($scope, $filter,$rootScope,contentService,$location,$anchorScroll) {
$scope.searchContents = [] ;
var filterList = function (list, keyword) {
return $filter('filter')(list, keyword);
};
var addToSearchContents = function (list,type){
_.each(list,function(item){
item.type = type;
$scope.searchContents.push(item);
});
};
$scope.init = function(){
var str = $location.absUrl();
$scope.searchKeyword = str.substring(str.indexOf("=") + 1,str.length);
!_.isEmpty($scope.searchKeyword)
{
// get all songs
contentService.getAllSongs().then(function (result) {
var filteredSongs = filterList(result.data.songs, $scope.searchKeyword);
addToSearchContents(filteredSongs,"song");
});
// get all people
contentService.getAllPeople().then(function (result) {
var filteredPeople = filterList(result.data.people, $scope.searchKeyword);
addToSearchContents(filteredPeople,"people");
});
_.each($scope.searchContents,function(item){
alert("item -> "+item.type);
});
}
};
$scope.init();
}]);
项(对象)
被添加到变量$scope.searchContents
中的addToSearchContents
中,但是如果我尝试访问/迭代推送到$scope.searchContents的所有对象。searchContents
与一起。每个看起来都是null
。但是我可以使用ng repeat
访问HTML页面中的所有内容,但不能在控制器中访问。我很困惑,我是不是遗漏了什么。您出现了错误,因为当您调用.each($scope.searchContents…
)时,数据尚未从异步调用中到达。addToSearchContents
尚未执行
使用,它将所有承诺组合成一个巨大的承诺。然后在所有承诺都得到解决后再做一些事情
注意:请记住将服务$q
注入控制器
我没有您的编码上下文,因此为您创建了一个类似的上下文。它说明了相同的想法。还有另一个变体,也包括:
似乎您缺少了一个if(
在测试之前)!\u.isEmpty($scope.searchKeyword)
。另一件事:使用角度内置函数而不是下划线。@mguimard-感谢您指出if
,并且我尝试了角度内置函数而不是下划线js…仍然无法迭代添加到控制器内部数组中的内容您提供的吗?contentService.getAllSongs()
和contentService.getAllPeople()
似乎是异步的东西。您正在迭代$scope.searchContents
,但它还没有填充。请查看的调用时间,每个…
,可能是getAllSongs()
尚未解析。@Joy,不,。中的每个
都在$q中的所有承诺后调用。所有
都已解析,因此getAllSongs
将被解析,然后
函数也将被解析。我看到更多信息,您可以看到您有相同的代码,但我将然后
函数放在$q.all
中:-)@Grundy nd@Joy-是的,由于异步调用以获取json数据,数组中的内容在解决之前没有填充…非常感谢:)
$q.all([
contentService.getAllSongs(),
contentService.getAllPeople()
]).then(function (result) {
// `result` is an array containing the results from the promises.
var filteredSongs = filterList(result[0].data.songs, $scope.searchKeyword);
addToSearchContents(filteredSongs,"song");
var filteredPeople = filterList(result[1].data.people, $scope.searchKeyword);
addToSearchContents(filteredPeople,"people");
_.each($scope.searchContents,function(item){
alert("item -> "+item.type);
});
});
$q.all([
// get all songs
contentService.getAllSongs().then(function (result) {
var filteredSongs = filterList(result.data.songs, $scope.searchKeyword);
addToSearchContents(filteredSongs,"song");
}),
// get all people
contentService.getAllPeople().then(function (result) {
var filteredPeople = filterList(result.data.people, $scope.searchKeyword);
addToSearchContents(filteredPeople,"people");
})]).then(function(){
// here all items already added so we can iterate it
_.each($scope.searchContents,function(item){
alert("item -> "+item.type);
});
});