Hassan Bendouj
Hassan Bendouj

Reputation: 313

separate files Unknown Provider

I'm trying separate my own services and controllers. I think I've done something wrong however because I continue to get the unknown provider error. This is my code for my app including service and controller.

TaskServices :

app = angular.module('TaskApp',[]);
app.factory('Tasks', function(Restangular) {
  return Restangular.service('tasks');
});

TaskControllers :

app = angular.module('TaskApp',[]);
//Task controller
app.controller('TaskCtrl',['Tasks', function($scope, Restangular, Tasks) {
  // loading data
  Tasks.getList().then(function(tasks){
    $scope.tasks = tasks;
  });
}]);
// Create new patient controller
app.controller('CreateTaskCtrl', function($scope, Restangular) {
});

Upvotes: 0

Views: 348

Answers (2)

ababashka
ababashka

Reputation: 2101

You should to inject your dependencies as the should be injected in the second parameter of your controllers definition.

I mean the next:

app.controller('TaskCtrl',['Tasks', function($scope, Restangular, Tasks)

will not work because of angular tries to inject only Tasks dependency, it does not know about Restangular and Tasks. After minification of the code above your $scope variable will be in the real a Tasks service.

Right syntax is next:

app.controller('TaskCtrl',[
  '$scope', 
  'Restangular', 
  'Tasks', 
  function($scope, Restangular, Tasks) {
    // code goes here
  }
]};

You should to inject your dependencies always as they go by the order.

Upvotes: 0

harishr
harishr

Reputation: 18065

you are defining module with same name at both places

TaskServices :
app = angular.module('TaskApp',[]);


TaskControllers :
app = angular.module('TaskApp',[]);

where as actually you should define it in one place, and just get it in another... app = angular.module('TaskApp');

Upvotes: 1

Related Questions