<!doctype html>
<html ng-app>
<head>
<meta charset="utf-8">
</head>
<body>
<div ng-controller="CommonController">
<div ng-controller="Controller1">
<p>{{greeting.text}},Angular</p>
<button ng-click="test1()">test1</button>
</div>
<div ng-controller="Controller2">
<p>{{greeting.text}},Angular</p>
<button ng-click="test2()">test2</button>
<button ng-click="commonFn()">通用</button>
</div>
</div>
</body>
<script src="js/angular-1.3.0.js"></script>
<script src="MVC3.js"></script>
</html>
通过$scope获取对象
function CommonController($scope){
$scope.commonFn=function(){
alert("这里是通用功能!");
};
}
function Controller1($scope) {
$scope.greeting = {
text: ‘Hello1‘
};
$scope.test1=function(){
alert("test1");
};
}
function Controller2($scope) {
$scope.greeting = {
text: ‘Hello2‘
};
$scope.test2=function(){
alert("test2");
}
}
<!doctype html>
<html ng-app>
<head>
<meta charset="utf-8">
</head>
<body>
<div>
<input ng-model="greeting.text"/>
<p>{{greeting.text}},Angular</p>
</div>
</body>
<script src="js/angular-1.3.0.js"></script>
</html>
1.ng-app规定了作用域为AngularJs解析 ng-model可以形成greeting.text模型对象
<!doctype html> <html ng-app="MyModule"> <head> <meta charset="utf-8"> </head> <body> <hello></hello> </body> <script src="js/angular-1.3.0.js"></script> <script src="HelloAngular_Directive.js"></script> </html>
通过Directive实现View的复用 <hello></hello>
var myModule = angular.module("MyModule", []);
myModule.directive("hello", function() {
return {
restrict: ‘E‘,
template: ‘<div>Hi everyone!</div>‘,
replace: true
}
});
原文:http://www.cnblogs.com/weizaiyes/p/6024716.html