Here is my 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)
{
// 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();
}]);
Items(objects) are added to the variable $scope.searchContents in addToSearchContents but if i try to access/iterate after all objects that are pushed to $scope.searchContents with _.each it seems to null. But i can access all the contents in HTML page with ng-repeat but not in controller. I am puzzled, am i missing something.
if(before testing!_.isEmpty($scope.searchKeyword). An other thing : use angular built-in functions instead of underscore.if, and i have tried angular inbuilt function instead of underscore js...still not able to iterate over contents added to array inside controllercontentService.getAllSongs()andcontentService.getAllPeople()seems to be asynchronous stuff. You're iterating the$scope.searchContentswhereas it's not filled yet.$q.all