简体   繁体   English

角度依赖注入误差

[英]Dependency injection error in angular

I am getting Uncaught Error: [$injector:modulerr] in a very strange manner. 我正在以一种非常奇怪的方式收到未捕获的错误:[$ injector:modulerr]

If I inject dependency in this manner it throws the above error. 如果我以这种方式注入依赖项,则会引发上述错误。

'use strict';
 var app = angular.module('myRoutes', ['ngRoute']);
 app.config(['$routeProvider'], function ($routeProvider) {

  }); 

But if I flip the above snippet in the below way, the error is gone. 但是,如果我以下面的方式翻转上面的代码片段,错误就消失了。

'use strict';
 var app = angular.module('myRoutes', ['ngRoute']);

 app.config(function ($routeProvider) {
    //no error
 });

I am using Angular v 1.3.1 我正在使用Angular v 1.3.1

Scripts including order. 脚本包括命令。

  • angular.js angular.js
  • angular-routes.js 角routes.js
  • myroutes.js myroutes.js
  • myCtrl.js myCtrl.js

Considering the minification in production environment, I can't go with the second way. 考虑到生产环境中的最小化,我不能采用第二种方法。

You have not closed config inline array annotation function correctly 您尚未正确关闭配置inline array annotation功能

app.config(['$routeProvider'], function ($routeProvider) {

should be 应该

//                     VVVVVVVVVV removed  `]`
app.config(['$routeProvider', function ($routeProvider) {

}]); //<-- close it here

You didn't close it right. 您没有正确关闭它。

'use strict';
 var app = angular.module('myRoutes', ['ngRoute']);
 app.config(['$routeProvider', function ($routeProvider) {

 }]); 

The recommended way of doing this is using the array notation. 推荐的方法是使用数组符号。

Read more here: https://docs.angularjs.org/guide/di 在此处阅读更多信息: https : //docs.angularjs.org/guide/di

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM