Search code examples
javascriptangularjsangularjs-routing

Unknown provider error - AngularJS App issue


var myApp = angular.module("MyApp", ['ngRoute']);

myApp.config(['$routeProvider',
  function($routeProvider) {
    $routeProvider.
      when('/', {
        templateUrl: 'partials/cart.php',
        controller: 'ctrl',
        resolve: {
            categories: function(cartService){
                console.log('inside resolve categories')
                return cartService.getCategories();
            }
          }           
      }).
      otherwise({
        redirectTo: '/'
      });
}]);



myApp.controller('ctrl', function (categories, $scope) {    
  $scope.items = categories;    
});


myApp.service("cartService", function ($http, $q) {     
    this.getCategories = function () {
        var deferred = $q.defer();    
        $http.get('js/categories.js')
            .then(function (response) {
              deferred.resolve(response.data);
            });

       return deferred.promise;     
    }    
});


myApp.run(['$rootScope',function($rootScope){    
    $rootScope.stateIsLoading = false;
    $rootScope.$on('$routeChangeStart', function(e, current, pre) {
        $rootScope.stateIsLoading = true;
        var fullRoute = current.$$route.originalPath;
        if(fullRoute == '/')
        {
          console.log('load categoreis and products')  
        }            
    });
    $rootScope.$on('$routeChangeSuccess', function() {
        $rootScope.stateIsLoading = false;
        console.log('route changed');
    });
    $rootScope.$on('$routeChangeError', function() {
        //catch error
    });    
}]);

I have placed the ng-app and ng-controller directives at the top of the HTML

<html lang="en" ng-app="MyApp" ng-controller="ctrl">

But when I run the HTML I get the following error

Error: [$injector:unpr] Unknown provider: categoriesProvider <- categories <- ctrl

How can I fix this ?

Edit: If I remove ng-controller="ctrl" from the HTML, then no errors


Solution

  • You got that error just because, you are using the same controller for both index.php and 'partials/cart.php'

    Create a separate controller for 'partials/cart.php' to resolve this problem

    Code Snippet:

    // Code goes here
    var app = angular.module('app', ['ngRoute']);
    
    app.controller('indexCtrl', function($scope) { 
      $scope.title = 'Header';
    });
    
    
    app.config(function($routeProvider) {
      $routeProvider.when('/', {
        template: "<ul><li ng-repeat='item in items'>{{item}}</li></ul>",
        controller: 'categoryCtrl',   
        resolve: {
            categories: function(cartService){
                return cartService.getCategories();
            }
          }
      });
      
    });
    
    app.controller('categoryCtrl', function (categories, $scope) {
      $scope.items = categories;
    });
    
    app.service("cartService", function() {
       this.getCategories = function() {
         return ['A', 'B', 'C'];
       };
    });
    <html ng-app="app">
    
      <head>
        <script data-require="[email protected]" data-semver="1.4.9" src="https://code.angularjs.org/1.4.9/angular.js"></script>
        <script data-require="[email protected]" data-semver="1.4.2" src="https://code.angularjs.org/1.4.2/angular-route.js"></script>
        <link rel="stylesheet" href="style.css" />
        <script src="script.js"></script>
      </head>
    
      <body ng-controller="indexCtrl">
        <h1>{{title}}</h1>
         <div ng-view></div>
      </body>
    
    </html>