html代码
<body ng-app="xmpl">
<div ng-controller="XmplController">
{{ greeting }}
</div>
</body>
js代码
angular.module('xmpl.service', []) //1.创建xmpl.service模块
.value('greeter', {
salutation: 'Hello',
localize: function(localization) {
this.salutation = localization.salutation;
},
greet: function(name) {
return this.salutation + ' ' + name + '!';
}
})
.value('user', {
load: function(name) {
this.name = name;
}
});
angular.module('xmpl.directive', []); //2.创建xmpl.directive模块
angular.module('xmpl.filter', []); //3.创建xmpl.filter模块
//4.创建xmpl, 并且引用其他模块代码
angular.module('xmpl', ['xmpl.service', 'xmpl.directive', 'xmpl.filter'])
.run(function(greeter, user) {
// This is effectively part of the main method initialization code
greeter.localize({
salutation: 'Bonjour'
});
user.load('World');
})
.controller('XmplController', function($scope, greeter, user) {
$scope.greeting = greeter.greet(user.name);
});
1.ng-app的作用
<html ng-app="invoice1">将html定义为一个angular容器。
2.angular.module("invoice1", [])模块的作用
angular.module以ng-app同名invoice1容器创建一个代码模块。将容器与模块对应。
目前的理解模块就是代码的封装吧。单一职责原则,一个模块专心负责一个业务类型。