Alex Man
Alex Man

Reputation: 4886

Unknown provider: $scopeProvider

I have created an application in AngularJS with having Skinny AngularJS Controllers. The application is working fine but the issue is that when I tried to access another controller through by using $controller('ControllerOne') I am getting the following exception.

JSFiddle

Error: [$injector:unpr] Unknown provider: $scopeProvider <- $scope
http://errors.angularjs.org/1.2.1/$injector/unpr?p0=%24scopeProvider%20%3C-%20%24scope
    at http://cdnjs.cloudflare.com/ajax/libs/angular.js/1.2.1/angular.js:78:12
    at http://cdnjs.cloudflare.com/ajax/libs/angular.js/1.2.1/angular.js:3451:19
    at Object.getService [as get] (http://cdnjs.cloudflare.com/ajax/libs/angular.js/1.2.1/angular.js:3578:39)
    at http://cdnjs.cloudflare.com/ajax/libs/angular.js/1.2.1/angular.js:3456:45
    at getService (http://cdnjs.cloudflare.com/ajax/libs/angular.js/1.2.1/angular.js:3578:39)
    at invoke (http://cdnjs.cloudflare.com/ajax/libs/angular.js/1.2.1/angular.js:3600:13)
    at Object.instantiate (http://cdnjs.cloudflare.com/ajax/libs/angular.js/1.2.1/angular.js:3636:23)
    at $get (http://cdnjs.cloudflare.com/ajax/libs/angular.js/1.2.1/angular.js:6639:28)
    at new <anonymous> (http://fiddle.jshell.net/_display/:47:22)
    at invoke (http://cdnjs.cloudflare.com/ajax/libs/angular.js/1.2.1/angular.js:3624:28)

My code is as given below

var mainApp = angular.module('mainApp', ['firstApp', 'secondApp']);

var firstApp = angular.module('firstApp', []);
firstApp.controller('ControllerOne', function ($scope) {
    this.name = "Messi";
});

var secondApp = angular.module('secondApp', []);
secondApp.controller('ControllerTwo', function ($scope, $controller) {
    $scope.ctrlOne = $controller('ControllerOne');
});

Can anyone please tell me some solution for this

Upvotes: 1

Views: 163

Answers (2)

dfsq
dfsq

Reputation: 193261

Not sure this is a very good idea to do it like you are doing, but here is a solution:

secondApp.controller('ControllerTwo', function ($scope, $controller) {
    $scope.ctrlOne = $controller('ControllerOne', {$scope: $scope});
});

The problem is that according to definition of ControllerOne it expects one dependency injection service: $scope. So you need to provide this service.

Demo: http://jsfiddle.net/oqnrrL8y/2/

Upvotes: 1

Vinay K
Vinay K

Reputation: 5572

$controller will instantiate the controller.

You need to pass the scope to it.

ctrl1 = $controller('ControllerOne', {$scope: $scope})

Here is the updated fiddle. http://jsfiddle.net/oqnrrL8y/1/

Upvotes: 1

Related Questions