1. Angularjs MVC
Model:數(shù)據(jù)模型層
View:視圖層,負責展示
Controller:業(yè)務(wù)邏輯和控制邏輯
優(yōu)點:代碼模塊化 代碼邏輯比較清晰萤厅、可移值性高怖现,后期維護方便藐石、代碼復用苇羡,
代碼規(guī)模越來越大的時候形导,切分職責是大勢所趨
缺點:運行效率稍微低一些
2.Angularjs $scope作用域
1.$scope多控制器單獨作用域
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
無標題文檔
無標題文檔無標題文檔
<script type="text/javascript" src="angular.min.js"></script>
</head>
<body>
<div ng-app="myApp">
<div ng-controller="firstController">{{name}}</div>
<div ng-controller="secondController">{{name}}</div>
</div>
<script type="text/javascript">
var app = angular.module("myApp", []);
app.controller('firstController',function($scope){$scope.name='張三'; });
app.controller('secondController',function($scope){ $scope.name='李四';});
</script>
</body>
</html>
無標題文檔
2.$rootScope服務(wù)
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="../angular.min.js"/>
</head>
<body>
<div ng-app="myApp">
<div ng-controller="firstController">
姓名:{{name}} </br>
年齡:{{age}}</div>
<div ng-controller="secondController">
姓名:{{name}}</br>
年齡:{{age}}
</div>
</div>
<script type="text/javascript">
var app = angular.module("myApp", []);
app.controller('firstController',function($scope,$rootScope){
$scope.name='張三';
$rootScope.age='30';
});
app.controller('secondController',function($scope){
$scope.name='李四';
})
</script>
</body>
</html>
3.Angularjs模塊的run方法
run方法初始化全局的數(shù)據(jù),只對全局作用域起作用 如$rootScope
<script type="text/javascript">
var test = angular.module('myApp',[]);
test.run(['$rootScope',function($rootScope){
$rootScope.name = 'hello world';
}]);
console.log(test);
</script>