AngularJS Error: $injector:unpr Unknown Provider

后端 未结 13 858
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-13 08:10

I\'m trying to build my own service by following the example in the documentation for the factory methodology. I think I\'ve done something wrong however because I continue

相关标签:
13条回答
  • 2020-12-13 08:52

    @ user2310334 I just tried this, a VERY basic example:

    HTML file

    <!DOCTYPE html>
    <html lang="en" ng-app="app">
    
    <head>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.6/angular.min.js" type="text/javascript"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.6/angular-route.min.js" type="text/javascript"></script>
        <script src="./app.js" type="text/javascript"></script>
    </head>
    
    <body>
        <div ng-controller="MailDetailCtrl">
        </div>
    </body>
    </html>
    

    The javascript file:

    var myApp= angular.module('app', ['ngRoute']);
    
    myApp.factory('mailService' , function () {
    return {
        getData : function(){
          var employees = [{name: 'John Doe', id: '1'},
            {name: 'Mary Homes', id: '2'},
            {name: 'Chris Karl', id: '3'}
          ];
    
          return employees;
        }
      };
    
    });
    
    
    myApp.controller('MailDetailCtrl',['$scope', 'mailService', function($scope, mailService) {
      alert(mailService.getData()[0].name);
    }]);
    

    And it works. Try it.

    0 讨论(0)
  • 2020-12-13 08:54

    In my case, I added a new service (file) to my app. That new service is injected in an existing controller. I did not miss new service dependency injection into that existing controller and did not declare my app module no more than one place. The same exception is thrown when I re-run my web app and my browser cache is not reset with a new service file codes. I simply refreshed my browser to get that new service file for browser cache, and the problem was gone.

    0 讨论(0)
  • 2020-12-13 08:57
    app.factory('getSettings', ['$http','$q' /*here!!!*/,function($http, $q) {
    

    you need to declare ALL your dependencies OR none and you forgot to declare $q .

    edit:

    controller.js : login, dont return ""

    0 讨论(0)
  • 2020-12-13 08:58

    Be sure that you load controller outsideapp.config. The following code may cause this error:

    app.config(["$stateProvider", "$urlRouterProvider", function ($stateProvider, $urlRouterProvider) {
           var AuthCtrl = require('components/auth/AuthCtrl'); //NOTICE HERE
           $stateProvider.state('login',{
                url: "/users/login",
                templateUrl: require("components/auth/login.tpl.html"),
                controller: AuthCtrl // ERROR
            })
    }))
    

    To fix this error, we must move AuthCtrl to outsideapp.config:

    var AuthCtrl = require('components/auth/AuthCtrl'); //NOTICE HERE
    app.config(["$stateProvider", "$urlRouterProvider", function ($stateProvider, $urlRouterProvider) {
           $stateProvider.state('login',{
                url: "/users/login",
                templateUrl: require("components/auth/login.tpl.html"),
                controller: AuthCtrl // WORK
            });
    }))
    
    0 讨论(0)
  • 2020-12-13 09:00

    also one of the popular reasons maybe you miss to include the service file in your page

    <script src="myservice.js"></script>
    
    0 讨论(0)
  • 2020-12-13 09:01

    Since this is the first Stackoverflow question that appears on Google when searching for Error: $injector:unpr Unknown Provider I'll add this here.

    Make sure that in your index.html any modules/dependencies are not being loaded after they are needed.

    For example in my code customFactory.factory.js begins like this:

    angular
       .module("app.module1")
       .factory("customFactory", customFactory);
    

    However the index.html looked like this:

        <script src="/src/client/app/customFactory.factory.js"></script>
        <script src="/src/client/app/module1.module.js"></script>
    

    Where it should've really looked like this:

        <script src="/src/client/app/module1.module.js"></script>
        <script src="/src/client/app/customFactory.factory.js"></script>
    

    Since customFactory.factory.js is declared as part of module1, it needs to be loaded before customFactory

    0 讨论(0)
提交回复
热议问题