nsr nsr - 3 months ago 12
AngularJS Question

AngularJS: how to call a function on page load

var init = function(){
$scope.getAllFriends($rootScope.rootName)
.then(function(data) {
console.log(data);
}, function(err) {
//error
});
}
init();
$scope.getAllFriends = function(name){
return friendService.getAllfriends(name)
}


Am new to angularjs, am trying to call a function on page load but am getting below error. Can anyone correct me where am wrong.

TypeError: $scope.getAllFriends is not a function

Answer

You are trying to call a function not yet declared: getAllFriends has not been declared at the time init it's executed.

Hence, you are getting "TypeError: $scope.getAllFriends is not a function".

try like this instead:

var init = function () {
        $scope.getAllFriends()
            .then(function (data) {
                console.log(data);
            }, function (err) {
                //error
            });
    }

$scope.getAllFriends = function (name) {
        return friendService.getAllfriends(name)
}
init();