未知的提供者错误 - AngularJS 应用程序问题 [英] Unknown provider error - AngularJS App issue

查看:29
本文介绍了未知的提供者错误 - AngularJS 应用程序问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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 的顶部

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">

但是当我运行 HTML 时出现以下错误

But when I run the HTML I get the following error

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

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

我该如何解决这个问题?

How can I fix this ?

如果我从 HTML 中删除 ng-controller="ctrl",则没有错误

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

推荐答案

你得到这个错误只是因为你对 index.php 和 'partials/cart.php' 使用了相同的控制器

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

为partials/cart.php"创建一个单独的控制器来解决这个问题问题

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

代码片段:

// 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="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>

这篇关于未知的提供者错误 - AngularJS 应用程序问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆