So, I am currently working on a solution of project, that needs 2 separate controllers and they need to share data. To be exact, they need to share data exactly once. I decided to go with service instead of using $rootScope, that would handle this data exchange. However, in my code, I cant even run anything, because I get
"Error: $injector:unpr Unknown Provider Unknown provider: simulationServiceProvider <- simulationService <- mainController"
I went through various answers to similar questions here and on other sites, but nothing helped. So why do I get injector error in my code which looks like this (notice, that controllers,module and service are all in separate files. I am also removing all the unnecessary code and leaving only code relevant to service usage)
html:
<!DOCTYPE html>
<html lang="sk" >
<head>
<meta charset="utf-8" />
<title>...</title>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.2/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
</head>
<body ng-app="App">
<div ng-controller="mainController">
....code here...
<div ng-controller="simulationController">
..more code here...
</div>
<script src="app.js"></script>
<script src="simulationService.js"></script>
<script src="mainController.js"></script>
<script src="simulationController.js"></script>
</body>
</html>
app.js contents:
var app = angular.module("App", []);
mainController.js content:
app.controller('mainController',['$scope','simulationService',function($scope, simulationService) {
...code...
$scope.startSimulation = function(value) {
simulationService.kNumber = $scope.kNumber;
simulationService.deltaFunction =$scope.deltaFunction;
simulationService.kSourceTracks = $scope.kSourceTracks;
simulationService.mode = $scope.stateEnum.MODE_2_SIMULATE;
simulationService.isActive = true;
}
...code...
}]);
simulationController.js content:
app.controller('simulationController',['$scope','simulationService',function ($scope,simulationService) {
if(simulationService.isActive){
$scope.kNumber = simulationService.kNumber;
$scope.kSourceTracks = simulationService.kSourceTracks;
$scope.mode = simulationService.mode;
if($scope.mode == 6){
$scope.deltaFunction = simulationService.deltaFunction;
}
}
}]);
And finally simulationService.js:
app.service('simulationService', function() {
var _kNumber;
var _kSourceTracks;
var _deltaFunction;
var _mode;
var _isActive = false;
this.kNumber = _kNumber;
this.kSourceTracks = _kSourceTracks;
this.deltaFunction =_deltaFunction;
this mode = _mode;
this.isActive = _isActive;
});
This setup gives me injection error and I have absolutely no idea why is this happening. I am not sure about the service contents being correct or that it does what I should, but firstly I have to get it injected and running before I can move forward and work on making it doing what I want. Thank you very much for any suggestions.