0xAX
0xAX

Reputation: 21837

Angular.JS unknown provider

I have Angular.js web application with service UserModel and controller - Users.

UserModel:

/*
 * User model service
 */
angular.module('my_mode_ext').factory('UserModel', [function(){
    // user model
    var model = [];

    //
    // Return users model
    //
    model.get = function(){
        // some object
        model = {.........};
        // return model
        return model;
    }

    return model;
}]);

I try to use it in controller:

var UsersController = ['$scope', '$rootScope', '$http', '$route',
  function($scope, $rootScope, $http, $route, UserModel, ngTableParams){
    console.log('UserModel ' + UserModel);
.....
}];

But got undefined in UserModel and error that:

Error: Unknown provider: UserModelProvider <- UserModel

How to fix it?

UPD. I found problem. Actually this service and controller are in other module than another application but i can't do just:

angular.module(my_main_module, ['my_mode_ext'])

Because i load this script in work time of web application. How to load module dynamically in angular application?

Thank you.

Upvotes: 0

Views: 1020

Answers (3)

Chandermani
Chandermani

Reputation: 42669

The controller has to be defined over the same module on which the UserModel service is defined or the Controller module should have dependency on the my_mode_ext module.

Seeing your code your controller definition becomes

angular.module('my_mode_ext').controller("UserController",['$scope', '$rootScope', '$http', '$route','UserModel','ngTableParams', function($scope, $rootScope, $http, $route, UserModel, ngTableParams){
    console.log('UserModel ' + UserModel);
}]);

Upvotes: 1

Dmitry Manannikov
Dmitry Manannikov

Reputation: 1154

Add string names before function

var UsersController = ['$scope', '$rootScope', '$http', '$route', 'UserModel', 'ngTableParams'

function($scope, $rootScope, $http, $route, UserModel, ngTableParams){
    console.log('UserModel ' + UserModel);
.....
}];

Upvotes: 1

Ajay Singh Beniwal
Ajay Singh Beniwal

Reputation: 19037

Syntax is not correct pls change to below

var UsersController = ['$scope', '$rootScope', '$http', '$route','UserModel','ngTableParams'
  function($scope, $rootScope, $http, $route, UserModel, ngTableParams){
    console.log('UserModel ' + UserModel);
.....
}];

Upvotes: 1

Related Questions