Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/438.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript angular js-无法访问控制器中推送到阵列的对象_Javascript_Angularjs_Angularjs Scope_Angularjs Ng Repeat - Fatal编程技术网

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);
        });
    });