Reputation: 53
My question is how can i inject a controller in an other?
this is my first controller:
define([ 'module', '{angular}/angular' ], function(module, angular) {
'use strict';
var listAnimcom = angular.module('listAnimcom', []);
listAnimcom.controller('listAnimcomCtrl', [ '$scope',
function($scope) {
$scope.testAnimcom="this controller is called";
} ]);
return {
angularModules : [ 'listAnimcom' ]
};
});
this is my second controller where i wante to inject the first:
define([ 'module', '{angular}/angular',
'{animcom}/modules/controllers/listAnimcom',
], function(module, angular) {
'use strict';
var plateformeDeTest = angular.module('plateformeDeTest', ['listAnimcom']);
plateformeDeTest.controller('plateformeDeTestCtrl', [ '$scope','listAnimcomCtrl',
function($scope,listAnimcomCtrl) {
$scope.typeActivite="polcom";
$scope.typeClient="plateforme";
} ]);
return {
angularModules : [ 'plateformeDeTest' ]
};
});
but it gives me this error:
angular.js:12798 Error: [$injector:unpr] http://errors.angularjs.org/1.4.12/$injector/unpr?p0=listAnimcomCtrlProvider%20%3C-%20listAnimcomCtr...
at angular.js:38
Any idea pls !!!
Upvotes: 0
Views: 40
Reputation: 222720
You cannot inject one controller to another controller.
In order to share the data among the controllers, consider using factory/service
.
Better to go through this answer to understand better on sharing data among controllers
.
Example using factory,
var app = angular.module("clientApp", [])
app.controller("TestCtrl",
function($scope,names) {
$scope.names =[];
$scope.save= function(){
names.add($scope.name);
}
});
app.controller("TestCtrl2",
function($scope,names) {
$scope.getnames = function(){
$scope.names = names.get();
}
});
app.factory('names', function(){
var names = {};
names.list = [];
names.add = function(message){
names.list.push({message});
};
names.get = function(){
return names.list;
};
return names;
});
<!doctype html>
<html >
<head>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.min.js"></script>
<script src="script.js"></script>
</head>
<body ng-app="clientApp">
<div ng-controller="TestCtrl">
<input type="text" ng-model="name">
<button ng-click="save()" > save</button>
</div>
<div ng-init="getnames()" ng-controller="TestCtrl2">
<div ng-repeat="name in names">
{{name}}
</div>
</div>
</body>
</html>
Upvotes: 1