javascript - angular js - unable to access objects pushed to array in controller -
here controller
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) { // songs contentservice.getallsongs().then(function (result) { var filteredsongs = filterlist(result.data.songs, $scope.searchkeyword); addtosearchcontents(filteredsongs,"song"); }); // 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(); }]);
items(objects)
added variable $scope.searchcontents
in addtosearchcontents
if try access/iterate after objects pushed $scope.searchcontents
_.each
seems null
. can access contents in html page ng-repeat
not in controller. puzzled, missing something.
yet variant, $q.all
:
$q.all([ // songs contentservice.getallsongs().then(function (result) { var filteredsongs = filterlist(result.data.songs, $scope.searchkeyword); addtosearchcontents(filteredsongs,"song"); }), // people contentservice.getallpeople().then(function (result) { var filteredpeople = filterlist(result.data.people, $scope.searchkeyword); addtosearchcontents(filteredpeople,"people"); })]).then(function(){ // here items added can iterate _.each($scope.searchcontents,function(item){ alert("item -> "+item.type); }); });
Comments
Post a Comment