2

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?

3
  • 1
    did u try using anonymous callback function in success Commented Apr 29, 2013 at 6:25
  • Wow. that works perfectly. So is it problem with angular or my definition? Commented Apr 29, 2013 at 6:38
  • 4
    $scope.handleSuccess is undefined when you passed it to .success(). Just put the handler above the rest of your code in the controller. Commented Apr 29, 2013 at 7:39

1 Answer 1

1

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

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.