AngularJS模块

时间:2023-12-30 14:46:08

方式一:

<body ng-app="myApp">
<div ng-controller="myCtrl1">
<h1>{{say()}}</h1>
<h1>{{age}}</h1>
</div>
<div ng-controller="myCtrl2">
<h1>{{say()}}</h1>
<h1>{{age}}</h1>
</div>
<script>
// var app = angular.module('myApp');//获取一个模块
var app = angular.module('myApp', []);//创建一个模块
app.controller('myCtrl1',function ($scope, $rootScope) {//在模块中添加控制器
$scope.say = function () {
return '大家好,我是Handsome!';
}
});
app.controller('myCtrl2', ['$scope', '$rootScope', function (scope, rootScope) {
scope.say = function () {
return '大家好,我是Zhang!';
}
rootScope.age = 23;
}]);
</script>
</body>

  方式二:

<body ng-app="myApp">
<div ng-controller="myCtrl1">
<h1>{{say()}}</h1>
<h1>{{age}}</h1>
</div>
<div ng-controller="myCtrl2">
<h1>{{say()}}</h1>
<h1>{{age}}</h1>
</div>
<script>
angular.module('myApp', [])
.controller('myCtrl1', function ($scope, $rootScope) {
$scope.say = function () {
return '你好,我是Handsome!';
}
})
.controller('myCtrl2', ['$scope', '$rootScope', function (scope, rootScope) {
scope.say = function () {
return '你好,我是Zhang!';
}
rootScope.age = 22;
}]);
</script>
</body>