1
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

2
  • you can use ng-init="init()" angular directive in html tag see Commented Aug 10, 2016 at 14:08
  • show your html code for further assistance Commented Aug 10, 2016 at 14:12

2 Answers 2

2

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();
Sign up to request clarification or add additional context in comments.

Comments

0

Use named function instead of object. Like this:

function getAllFriends(name){...}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.