AngularJS 作用域詳解及示例代碼
范圍扮演其視圖連接控制器的角色一個(gè)特殊的JavaScript對(duì)象。范圍包含了模型數(shù)據(jù)。在控制器,模型數(shù)據(jù)通過(guò)$scope對(duì)象訪問(wèn)。
<script>
var mainApp = angular.module("mainApp", []);
mainApp.controller("shapeController", function($scope) {
$scope.message = "In shape controller";
$scope.type = "Shape";
});
</script>
以下是在上面的例子中需要考慮的重要問(wèn)題。
$scope被作為第一個(gè)參數(shù)在其構(gòu)造器確定指標(biāo)到控制器。
$scope.message 和 $scope.type 是它們?cè)贖TML頁(yè)面中所用的模型。
我們已經(jīng)設(shè)置模型的值將反映應(yīng)用程序模塊的控制器shapeController中。
我們可以在$scope定義函數(shù)功能。
繼承范圍
范圍是特定的控制器。如果我們定義嵌套的控制器,然后控制器子將繼承其父控制的范圍。
<script>
var mainApp = angular.module("mainApp", []);
mainApp.controller("shapeController", function($scope) {
$scope.message = "In shape controller";
$scope.type = "Shape";
});
mainApp.controller("circleController", function($scope) {
$scope.message = "In circle controller";
});
</script>
以下是在上面的例子中需要考慮的重要問(wèn)題。
我們?cè)趕hapeController設(shè)定模型的值。
我們覆蓋的子控制器circleController消息。當(dāng)“消息”內(nèi)的控制器circleController的模塊使用時(shí),將用于重寫(xiě)的消息。
例子
下面的例子將展示上述所有指令。
testAngularJS.html
<html>
<head>
<title>Angular JS Forms</title>
</head>
<body>
<h2>AngularJS Sample Application</h2>
<div ng-app="mainApp" ng-controller="shapeController">
<p>{{message}} <br/> {{type}} </p>
<div ng-controller="circleController">
<p>{{message}} <br/> {{type}} </p>
</div>
<div ng-controller="squareController">
<p>{{message}} <br/> {{type}} </p>
</div>
</div>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular.min.js"></script>
<script>
var mainApp = angular.module("mainApp", []);
mainApp.controller("shapeController", function($scope) {
$scope.message = "In shape controller";
$scope.type = "Shape";
});
mainApp.controller("circleController", function($scope) {
$scope.message = "In circle controller";
});
mainApp.controller("squareController", function($scope) {
$scope.message = "In square controller";
$scope.type = "Square";
});
</script>
</body>
</html>
結(jié)果
在Web瀏覽器打開(kāi)textAngularJS.html??吹浇Y(jié)果如下。

以上就是對(duì)AngularJS 作用域的資料整理,后續(xù)繼續(xù)補(bǔ)充相關(guān)資料,謝謝大家對(duì)本站的支持!
相關(guān)文章
AngularJS+bootstrap實(shí)現(xiàn)動(dòng)態(tài)選擇商品功能示例
這篇文章主要介紹了AngularJS+bootstrap實(shí)現(xiàn)動(dòng)態(tài)選擇商品功能,涉及AngularJS指令、事件響應(yīng)及頁(yè)面元素動(dòng)態(tài)操作相關(guān)技巧,需要的朋友可以參考下2017-05-05
利用AngularJs實(shí)現(xiàn)京東首頁(yè)輪播圖效果
這篇文章給大家介紹了如何利用AngularJs實(shí)現(xiàn)京東首頁(yè)輪播圖的效果,本文通過(guò)示例代碼詳細(xì)介紹了實(shí)現(xiàn)過(guò)程,對(duì)大家學(xué)習(xí)AngularJS具有一定參考借鑒價(jià)值,有需要的朋友們可以參考借鑒。2016-09-09
Angularjs實(shí)現(xiàn)數(shù)組隨機(jī)排序的方法
今天小編就為大家分享一篇Angularjs實(shí)現(xiàn)數(shù)組隨機(jī)排序的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-10-10
詳解在Angularjs中ui-sref和$state.go如何傳遞參數(shù)
這篇文章主要介紹了詳解在Angularjs中ui-sref和$state.go如何傳遞參數(shù),詳細(xì)的介紹了ui-sref和$state.go的區(qū)別和如何傳參,有興趣的可以了解下2017-04-04
angular2 組件之間通過(guò)service互相傳遞的實(shí)例
今天小編就為大家分享一篇angular2 組件之間通過(guò)service互相傳遞的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-09-09
詳解Angular 4.x 動(dòng)態(tài)創(chuàng)建組件
本篇文章主要介紹了詳解Angular 4.x 動(dòng)態(tài)創(chuàng)建組件,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-04-04
angularjs實(shí)現(xiàn)搜索的關(guān)鍵字在正文中高亮出來(lái)
這篇文章主要介紹了angularjs實(shí)現(xiàn)搜索的關(guān)鍵字在正文中高亮出來(lái),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06

