Asp.net-Mvc

AngularJS 在發布模式下不工作(縮小)

  • March 27, 2014

我在一個項目中有 AngularJS 和 Bootstrap 的 Angular 指令,這兩個指令都是通過 Nuget 添加的。

編譯 debug=“true”時,一切正常,但是一旦我將編譯調試更改為false,Angular 就會停止工作,並且我在 Chrome 的控制台中收到以下錯誤:

Error: [$injector:unpr] http://errors.angularjs.org/1.2.15/$injector/unpr?p0=nProvider%20%3C-%20n
at Error (native)
at http://localhost:10366/bundles/angular?v=Bi8OwYOhmsGdeIKC46ZQQdIHX1faTIVR48lG9QG3GBw1:1:448
at http://localhost:10366/bundles/angular?v=Bi8OwYOhmsGdeIKC46ZQQdIHX1faTIVR48lG9QG3GBw1:1:14726
at Object.i [as get] (http://localhost:10366/bundles/angular?v=Bi8OwYOhmsGdeIKC46ZQQdIHX1faTIVR48lG9QG3GBw1:1:13802)
at http://localhost:10366/bundles/angular?v=Bi8OwYOhmsGdeIKC46ZQQdIHX1faTIVR48lG9QG3GBw1:1:14801
at i (http://localhost:10366/bundles/angular?v=Bi8OwYOhmsGdeIKC46ZQQdIHX1faTIVR48lG9QG3GBw1:1:13802)
at r (http://localhost:10366/bundles/angular?v=Bi8OwYOhmsGdeIKC46ZQQdIHX1faTIVR48lG9QG3GBw1:1:14014)
at Object.instantiate (http://localhost:10366/bundles/angular?v=Bi8OwYOhmsGdeIKC46ZQQdIHX1faTIVR48lG9QG3GBw1:1:14185)
at http://localhost:10366/bundles/angular?v=Bi8OwYOhmsGdeIKC46ZQQdIHX1faTIVR48lG9QG3GBw1:1:30669
at http://localhost:10366/bundles/angular?v=Bi8OwYOhmsGdeIKC46ZQQdIHX1faTIVR48lG9QG3GBw1:1:23558 angular?v=Bi8OwYOhmsGdeIKC46ZQQdIHX1faTIVR48lG9QG3GBw1:1
(anonymous function) angular?v=Bi8OwYOhmsGdeIKC46ZQQdIHX1faTIVR48lG9QG3GBw1:1

我所有的其他 Javascript/CSS 都按預期工作。一旦縮小,什麼可能導致 Angular 停止工作?這是一個已知的問題?

謝謝

當值設置為. 角度教程包括這個關於縮小的說明:compilation debug``"false"

由於 Angular 會根據控制器建構子的參數名稱推斷控制器的依賴關係,因此如果您要縮小 JavaScript 程式碼

$$ your $$控制器,它的所有函式參數也將被縮小,並且依賴注入器將無法正確辨識服務。

解決這個問題的最常用技術是內聯括號表示法,例如:

phonecatApp.controller('PhoneListCtrl', ['$scope', '$http', function($scope, $http) {...}]);

這個 SO 問題解釋了縮小安全語法的重要性:Angularjs minify best practice

引用自:https://stackoverflow.com/questions/22676454