Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

factory is undefined in angularjs

Tags:

angularjs

I am trying to create a factory in AngularJS based on this article. But I am getting an error saying "dataFactory" is undefined in my controller. My code looks like this:

MyApp.factory('dataFactory', ['$http', function($http)
    {
        var urlBase='/someurl/roles/';
        var dataFactory={};

        dataFactory.getRoles=function()
        {
            return $http.get(urlBase+'getlist');
        };

        dataFactory.addRole=function(roleName, roleCss)
        {
            return $http.get(urlBase+'add&name='+roleName+'&css='+roleCss);
        };

        dataFactory.updateRole=function(role)
        {
          return $http.get(urlBase+edit&id='+role.id+'&name='+role.name+'&css='+role.css');
        };

        dataFactory.deleteRole=function(role)
        {
            return $http.get(urlBase+'remove&id='+role.id);
        };

        return dataFactory;
    }]);

MyApp.controller('RoleCtrl',['$scope','$http','dataFactory', function($scope,$http,$rootScope,dataFactory) {
     dataFactory.getRoles().then(function(res){
        $scope.roles = res.data.result;   
      });
 }]);

What am I doing wrong?

like image 657
ThomasD Avatar asked Nov 15 '13 23:11

ThomasD


1 Answers

You're missing the string version of '$rootScope'

So instead of

MyApp.controller('RoleCtrl',['$scope','$http','dataFactory', function($scope,$http,$rootScope,dataFactory) {

use (note the addition of '$rootScope'):

MyApp.controller('RoleCtrl',['$scope','$http','$rootScope','dataFactory', function($scope,$http,$rootScope,dataFactory) {

dataFactory was going into $rootscope since you had a misalignment of parameters. And then there was nothing to send in to dataFactory. Thus your error.

like image 107
KayakDave Avatar answered Nov 18 '22 05:11

KayakDave