-2

what i need

js code

 var app = angular.module('myApp', []);
 app.service('productService', function() {
   var productList = [];

   var addProduct = function(newObj) {
     productList.push(newObj);
   };

   var getProducts = function(){
    return productList;
   };

   return {
     addProduct: addProduct,
     getProducts: getProducts
   };
 });




app.controller('parentController', function ($scope,productService) {
  $scope.change = function () {
    alert($scope.value);
  }
});

error

error: [$injector:unpr] Unknown provider: productServiceProvider <- productService <- parentController 
  • i need to pass data from controller to another .

  • whereas i don"t understand why its producing such errors.

Community
  • 1
  • 1
user2818060
  • 835
  • 5
  • 19
  • 41

2 Answers2

1

This error results from the $injector being unable to resolve a required dependency.
To fix this, make sure the dependency is defined and spelled correctly.

I have two guess for this:

  • You fix a service name typo, while copy code to your question Fiddle.
    Just check your actual service name

  • You redeclare angular.module Example

Example:

var app = angular.module('myApp', []);
app.service('productService', function() {
  return {};
});

app = angular.module('myApp', []); // <- module redeclared
app.controller('parentController', function ($scope,productService) {
  $scope.value = 'World'
});    

angular.module('myApp', []) creates new instance of module without knowing previous declared providers.
You can retrieve it for subsequent use with angular.module('myApp').

vp_arth
  • 14,461
  • 4
  • 37
  • 66
  • i cannot add drop down values in addproduct() function :undefined – user2818060 Jun 10 '16 at 09:40
  • vp_arth you have explain in html i need to pass value in another controller :ie i m making http request so i need to pass value in api: https://jsfiddle.net/wwvgfcp3/7/ – user2818060 Jun 10 '16 at 10:11
  • Just inject your service to that controller and call `productService.getProducts()` [fiddle](https://jsfiddle.net/vp_arth/wwvgfcp3/9/). This all is unrelated to `unknown provider` error. Explain please, what it was? Redeclaring? Typo? – vp_arth Jun 10 '16 at 10:14
  • in console.log it still [] empty array i don"t need to display in html i need to pass value in http://link?id=+value – user2818060 Jun 10 '16 at 10:20
  • it's because controller code is running while application initialization only, you have empty array here. You can use `$scope.products` or even call `getProducts()` again everywhere you need. – vp_arth Jun 10 '16 at 10:26
  • Please start new question if you want to ask something else :) – vp_arth Jun 10 '16 at 10:27
  • can you give some code snippet with respect in communication within controller : jsfiddle – user2818060 Jun 10 '16 at 10:31
  • 1
    ok, there is sub-pub snippet: [fiddle](https://jsfiddle.net/vp_arth/wwvgfcp3/11/). Controller subcribes for changes, and receives changed list any time new product added. – vp_arth Jun 10 '16 at 10:34
-1

you have declared service before module declearation. so it is not added to your module

follow these steps

var app = angular.module('myApp', []);

then create controllers and service

app.service('productService', function() { ....

and

app.controller('parentController', function ($scope,productService) {...

I have change your fiddle example check Link,

I have created another controller which share same service.

Jeetendra Chauhan
  • 1,977
  • 2
  • 15
  • 21