定义一个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>