詳解AngularJS中的作用域
范圍扮演其視圖連接控制器的角色一個特殊的JavaScript對象。范圍包含了模型數(shù)據(jù)。在控制器,模型數(shù)據(jù)通過$scope對象訪問。
<script> var mainApp = angular.module("mainApp", []); mainApp.controller("shapeController", function($scope) { $scope.message = "In shape controller"; $scope.type = "Shape"; }); </script>
以下是在上面的例子中需要考慮的重要問題。
- $scope被作為第一個參數(shù)在其構(gòu)造器確定指標(biāo)到控制器。
- $scope.message 和 $scope.type 是它們在HTML頁面中所用的模型。
- 我們已經(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>
以下是在上面的例子中需要考慮的重要問題。
- 我們在shapeController設(shè)定模型的值。
- 我們覆蓋的子控制器circleController消息。當(dāng)“消息”內(nèi)的控制器circleController的模塊使用時,將用于重寫的消息。
例子
下面的例子將展示上述所有指令。
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瀏覽器打開textAngularJS.html??吹浇Y(jié)果如下。
- Angularjs全局變量被作用域監(jiān)聽的正確姿勢
- 詳細(xì)談?wù)凙ngularJS的子級作用域問題
- 關(guān)于angularJs指令的Scope(作用域)介紹
- 淺談angularJS 作用域
- 詳解JavaScript的AngularJS框架中的作用域與數(shù)據(jù)綁定
- AngularJS實現(xiàn)單獨作用域內(nèi)的數(shù)據(jù)操作
- AngularJS入門教程之Scope(作用域)
- AngularJS Controller作用域
- 詳解angularjs中的隔離作用域理解以及綁定策略
- 詳談AngularJs 控制器、數(shù)據(jù)綁定、作用域
- AngularJS中的作用域?qū)嵗治?/a>
相關(guān)文章
angular6.0使用教程之父組件通過url傳遞id給子組件的方法
這篇文章主要介紹了angular6.0使用教程之父組件通過url傳遞id給子組件的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-06-06Angular 5.x 學(xué)習(xí)筆記之Router(路由)應(yīng)用
本篇文章主要介紹了Angular 5.x 學(xué)習(xí)筆記之Router(路由)應(yīng)用,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-04-04Angular.js通過自定義指令directive實現(xiàn)滑塊滑動效果
這篇文章主要給大家介紹了關(guān)于Angular.js如何通過自定義指令directive實現(xiàn)滑塊滑動的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用angularjs具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起看看吧。2017-10-10BootStrap+Angularjs+NgDialog實現(xiàn)模式對話框
在完成一個后臺管理系統(tǒng)時,需要用表格顯示注冊用戶的信息。但是用戶地址太長了,不好顯示。所以想做一個模式對話框,點擊詳細(xì)地址按鈕時,彈出對話框,顯示地址。下面小編給大家分享下實現(xiàn)方法,一起看下吧2016-08-08Angular統(tǒng)一注入器unified injector簡化依賴關(guān)系管理
這篇文章主要為大家介紹了Angular統(tǒng)一注入器unified injector簡化依賴關(guān)系管理的使用方法實例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-10-10