定義一個Controller,通常方法是如下代碼,但是代碼壓縮的過程中function裡面的參數有可能會變化,$scope有可能會變成$sc,或者是其他(這裡的變化不可控制),一旦變化, 下面綁定的值就會出錯。
var app = angular.module("myApp", []);
app.controller('firstController',function($scope){
$scope.name='張三';
});
為了解決這個問題,在function外面加[ ],傳入字元串,如下代碼所示,因為字元串在壓縮的過程中不會改變。
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>無标題文檔</title>
<script type="text/javascript" src="angular.min.js"></script>
</head>
<body>
<div ng-app="myApp">
<div ng-controller="firstController">
{{name}}
{{age}}
<div ng-controller="secondController">
{{name}}
{{age}}
</div>
</div>
</div>
<script type="text/javascript">
var app = angular.module("myApp", []);
app.controller('firstController',['$scope',function($scope){
$scope.name='張三';
}]);
app.controller('secondController',['$scope','$rootScope',function($scope,$rootScope){
$scope.name='李四';
$rootScope.age='30';
}]);
console.log(app);
</script>
</body>
</html>