0

I know this duplicate but doesn't matches with anyone that's why I have posted this one. I have one controller and one factory. Factory is working fine and getting response in console but when i called a function of factory a error has been printed. Please check below code: Controller:

var app = angular.module('rsplApp',['ngRoute', 'ngCookies',  'ngResource',  'ngSanitize', 'ngValidate']);
app.controller('AddProjectController',['$scope', '$cookies', '$rootScope', '$location','Technologies', function ($scope, $cookies, $rootScope, $location, Technologies){        
        Technologies.loadTech().then(function(techRes){ 
        $scope.choices = techRes;//console.log(techRes);
        })
}])

Factory:

app.factory("Technologies",function($http){ 
var TechCat = {};   
TechCat.loadTech = function(){          
        $http({
                    method  : 'POST',
                    url     : 'api/v1/technologies.php',
                    data    : '',  // pass in data as strings       
                    headers : { 'Content-Type': 'application/x-www-form-urlencoded' }

                }).success(function(response){
                        //console.log(response);
                        return response;
                    }).error(function(erronrmsg){
                        return erronrmsg;               

                        })  

}

return TechCat;
})

now when i refresh page I received this error: "Error: Technologies.loadTech(...) is undefined @http://localhost/vk-angular/scripts/controllers/AddProjectController.js:4:4"

1
  • Correct your indenting and add semicolons for a start. Commented Jan 25, 2016 at 6:24

2 Answers 2

1

var app = angular.module('rsplApp',['ngRoute', 'ngCookies',  'ngResource',  'ngSanitize', 'ngValidate']);
app.controller('AddProjectController',['$scope', '$cookies', '$rootScope', '$location','Technologies', function ($scope, $cookies, $rootScope, $location, Technologies){        
        Technologies.loadTech().then(function(techRes){ 
        $scope.choices = techRes;//console.log(techRes);
        })
}]);

app.factory("Technologies",function($http){ 
var TechCat = {};   
TechCat.loadTech = function(){          
      return  $http({
                    method  : 'POST',
                    url     : 'api/v1/technologies.php',
                    data    : '',  // pass in data as strings       
                    headers : { 'Content-Type': 'application/x-www-form-urlencoded' }

                }).success(function(response){
                        //console.log(response);
                        return response;
                    }).error(function(erronrmsg){
                        return erronrmsg;               

                        })  

};

return TechCat;
});

Sign up to request clarification or add additional context in comments.

2 Comments

I think semi colon ; miss at service obj function, and return $http then only you will get promise, means you can use 'then' function inside controller
thankyou @saikumar you are right i have missed semi colon at service obj function
0

You forgot to return the $http promise from the loadTech() function. So, implicitly undefined is returned. But in your controller you are using loadTech() as if it returned a promise.

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.