为什么我的 angular 应用程序在调试模式下工作正常,而不是在优化 js 包时

Why my angular app works fine in debug mode and not when js bundle is optimized

我有两个申请 angularjs。我不得不从一个页面中取出一些页面才能迁移到第二个页面。 由于我使用 System.Web.Optimization 激活了捆绑优化,应用程序不再运行。 这是控制台的状态:

Error: [$injector:modulerr] http://errors.angularjs.org/1.5.9/$injector/modulerr?p0=MyApp&p1=Error%3A%20%5B%24injector%3Aunpr%5D%20http%3A%2F%2Ferrors.angularjs.org%2F1.5.9%2F%24injector%2Funpr%3Fp0%3Dn%0A%20%20%20at%20Anonymous%20function%20(http%3A%2F%2Flocalhost%3A30100%2Fbundles%2Fvendor.js%3Fv%3D0CC4LNBLFTXMcD7A3WwKd4pdBcv7gCYsWZTADZBarKU1%3A1%3A328163)%0A%20%20%20at%20r%20(http%3A%2F%2Flocalhost%3A30100%2Fbundles%2Fvendor.js%3Fv%3D0CC4LNBLFTXMcD7A3WwKd4pdBcv7gCYsWZTADZBarKU1%3A1%3A326874)%0A%20%20%20at%20u%20(http%3A%2F%2Flocalhost%3A30100%2Fbundles%2Fvendor.js%3Fv%3D0CC4LNBLFTXMcD7A3WwKd4pdBcv7gCYsWZTADZBarKU1%3A1%3A327109)%0A%20%20%20at%20invoke%20(http%3A%2F%2Flocalhost%3A30100%2Fbundles%2Fvendor.js%3Fv%3D0CC4LNBLFTXMcD7A3WwKd4pdBcv7gCYsWZTADZBarKU1%3A1%3A327193)%0A%20%20%20at%20r%20(http%3A%2F%2Flocalhost%3A30100%2Fbundles%2Fvendor.js%3Fv%3D0CC4LNBLFTXMcD7A3WwKd4pdBcv7gCYsWZTADZBarKU1%3A1%3A326368)%0A%20%20%20at%20Anonymous%20function%20(http%3A%2F%2Flocalhost%3A30100%2Fbundles%2Fvendor.js%3Fv%3D0CC4LNBLFTXMcD7A3WwKd4pdBcv7gCYsWZTADZBarKU1%3A1%3A326499)%0A%20%20%20at%20t%20(http%3A%2F%2Flocalhost%3A30100%2Fbundles%2Fvendor.js%3Fv%3D0CC4LNBLFTXMcD7A3WwKd4pdBcv7gCYsWZTADZBarKU1%3A1%3A309792)%0A%20%20%20at%20b%20(http%3A%2F%2Flocalhost%3A30100%2Fbundles%2Fvendor.js%3Fv%3D0CC4LNBLFTXMcD7A3WwKd4pdBcv7gCYsWZTADZBarKU1%3A1%3A326275)%0A%20%20%20at%20yr%20(http%3A%2F%2Flocalhost%3A30100%2Fbundles%2Fvendor.js%3Fv%3D0CC4LNBLFTXMcD7A3WwKd4pdBcv7gCYsWZTADZBarKU1%3A1%3A328292)%0A%20%20%20at%20o%20(http%3A%2F%2Flocalhost%3A30100%2Fbundles%2Fvendor.js%3Fv%3D0CC4LNBLFTXMcD7A3WwKd4pdBcv7gCYsWZTADZBarKU1%3A1%3A316546)

   at Anonymous function (http://localhost:30100/bundles/vendor.js?v=0CC4LNBLFTXMcD7A3WwKd4pdBcv7gCYsWZTADZBarKU1:1:326593)
   at t (http://localhost:30100/bundles/vendor.js?v=0CC4LNBLFTXMcD7A3WwKd4pdBcv7gCYsWZTADZBarKU1:1:309792)
   at b (http://localhost:30100/bundles/vendor.js?v=0CC4LNBLFTXMcD7A3WwKd4pdBcv7gCYsWZTADZBarKU1:1:326275)
   at yr (http://localhost:30100/bundles/vendor.js?v=0CC4LNBLFTXMcD7A3WwKd4pdBcv7gCYsWZTADZBarKU1:1:328292)
   at o (http://localhost:30100/bundles/vendor.js?v=0CC4LNBLFTXMcD7A3WwKd4pdBcv7gCYsWZTADZBarKU1:1:316546)
   at fo (http://localhost:30100/bundles/vendor.js?v=0CC4LNBLFTXMcD7A3WwKd4pdBcv7gCYsWZTADZBarKU1:1:317026)
   at Anonymous function (http://localhost:30100/bundles/vendor.js?v=0CC4LNBLFTXMcD7A3WwKd4pdBcv7gCYsWZTADZBarKU1:1:1121209)
   at a (http://localhost:30100/bundles/vendor.js?v=0CC4LNBLFTXMcD7A3WwKd4pdBcv7gCYsWZTADZBarKU1:1:36555)
   at h.add (http://localhost:30100/bundles/vendor.js?v=0CC4LNBLFTXMcD7A3WwKd4pdBcv7gCYsWZTADZBarKU1:1:36865)
   at i.prototype.ready (http://localhost:30100/bundles/vendor.js?v=0CC4LNBLFTXMcD7A3WwKd4pdBcv7gCYsWZTADZBarKU1:1:12694)

建议?

使用严格的依赖注入

使用 Implicit Annotation,代码会在缩小时中断。

来自文档:

Implicit Annotation

Careful: If you plan to minify your code, your service names will get renamed and break your app.

您可以在与 ng-app 相同的元素上添加一个 ng-strict-di 指令以选择进入严格的 DI 模式。

<body ng-app="myApp" ng-strict-di>

只要服务尝试使用隐式注释,严格模式就会引发错误。

有关详细信息,请参阅