未知的提供程序错误 - AngularJS App问题

时间:2016-02-17 10:14:48

标签: javascript angularjs angularjs-routing

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
    });    
}]);

我已将ng-app和ng-controller指令放在HTML的顶部

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

但是当我运行HTML时,我收到以下错误

  

错误:[$ injector:unpr]未知提供者:categoriesProvider&lt; -   类别&lt; - ctrl

我该如何解决这个问题?

编辑:如果我从HTML中删除ng-controller =“ctrl”,则没有错误

2 个答案:

答案 0 :(得分:1)

你得到那个错误只是因为你对index.php和&#39; partials / cart.php&#39;

使用相同的控制器
  

为&part; / cart.php&#39;创建一个单独的控制器。解决这个问题   问题

代码段:

&#13;
&#13;
// 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'];
   };
});
&#13;
<html ng-app="app">

  <head>
    <script data-require="angular.js@1.4.9" data-semver="1.4.9" src="https://code.angularjs.org/1.4.9/angular.js"></script>
    <script data-require="angular-route@1.4.2" 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>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

您必须定义在控制器'ctrl'中注入的类别服务/工厂。