在实际的开发中往往离不开js的代码压缩,因为这样可以减轻服务器的压力,附上例子:
demo.html
<!DOCTYPE html>
<html lang="en" ng-app="myapp">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="js/angular.min.js"></script>
</head>
<body>
<div ng-controller="app1">{{name}}</div>
<div ng-controller="app2">{{name}}</div>
</body>
<script src="js/demo.js"></script>
</html>
demo.js
var app = angular.module('myapp',[]);
//这种方式定义的控制器如果在上线的项目中是会出现问题的,因为压缩会将形参里面的东西给破坏掉,$scope变成了其他参数名称
app.controller('app1',function($scope){
$scope.name = 'app1';
});
//这种方式定义的不会受压缩工具的任何影响
app.controller('app2',['$scope',function($s){
$s.name = 'app2';
}]);
压缩之后,demo.min.js
var app=angular.module("myapp",[]);app.controller("app1",function(a){a.name="app1"});app.controller("app2",["$scope",function(a){a.name="app2"}]);
测试结果:
未压缩之前,直接可以使用,
压缩之后,报错 Error: [$injector:unpr] Unknown provider: aProvider <- a <- app1
angular_1.4.4.20151215_21217.js:577 Error: [$injector:unpr] Unknown provider: aProvider <- a <- app1
http://errors.angularjs.org/1.4.4/$injector/unpr?p0=aProvider%20%3C-%20a%20%3C-%20app1
at angular_1.4.4.20151215_21217.js:6
at angular_1.4.4.20151215_21217.js:210
at Object.getService [as get] (angular_1.4.4.20151215_21217.js:226)
at angular_1.4.4.20151215_21217.js:210
at getService (angular_1.4.4.20151215_21217.js:226)
at Object.invoke (angular_1.4.4.20151215_21217.js:230)
at extend.instance (angular_1.4.4.20151215_21217.js:442)
at nodeLinkFn (angular_1.4.4.20151215_21217.js:374)
at compositeLinkFn (angular_1.4.4.20151215_21217.js:323)
at compositeLinkFn (angular_1.4.4.20151215_21217.js:323)
推荐压缩工具"JSCompress"