Javascript – AngularJS undefined is not a function

angularjshttpjavascript

Im trying out a simple http app using Angularjs.
My code is

var studentApp = angular.module('studentApp',['ui.bootstrap']);
studentApp.factory('studentSession', function($http){
    return {
        getSessions: function() {
            return $http.post('/services', { 
                type : 'getSource',
                ID    : 'TP001'
            });
        }
    }
});

studentApp.controller('studentMenu',function($scope, studentSession){
    studentSession.getSessions().success($scope.handleSuccess);
    $scope.handleSuccess = function(data, status) {
        console.log(data);
    };
});

While trying to run above code, i get the error Undefined is not a function in chrome . Searching through stackoverflow the recommended fix is to provide scope for function. Error persists even after scoping the function.

Suspected problem with corrupt angular file, Since the code works fine if i move the getSession code inside controller. But the error persists on angular version 1.0.6 and 1.1.4(both uncompressed and minified version).

The error varies in firefox. its fn is not a function with uncompressed angularjs and b is not a function with minified angularjs.

any suggestion on how to fix this issue?

Best Solution

Oki so based on the comments the following code works

var studentApp = angular.module('studentApp',['ui.bootstrap']);
studentApp.factory('studentSession', function($http){
    return {
        getSessions: function() {
            return $http.post('/services', { 
                type : 'getSource',
                ID    : 'TP001'
            });
        }
    }
});

studentApp.controller('studentMenu',function($scope, studentSession){
    //------Code Change---------
    //Moved before function call.
    $scope.handleSuccess = function(data, status) {
        console.log(data);
    }; 
    studentSession.getSessions().success($scope.handleSuccess);
});