国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

詳解AngularJS中的作用域

2019-11-20 12:13:43
字體:
供稿:網(wǎng)友

 范圍扮演其視圖連接控制器的角色一個特殊的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。看到結(jié)果如下。

2015617110218233.jpg (560×429)

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 旬邑县| 绥化市| 菏泽市| 东辽县| 清涧县| 北流市| 牡丹江市| 马龙县| 青海省| 同德县| 女性| 宝鸡市| 时尚| 玉树县| 礼泉县| 晋宁县| 宜宾县| 南川市| 长葛市| 阳城县| 隆昌县| 彰化市| 涪陵区| 天等县| 库尔勒市| 樟树市| 沈丘县| 南雄市| 巴彦淖尔市| 得荣县| 达孜县| 宁明县| 新安县| 巴林左旗| 潞城市| 称多县| 昌平区| 寻甸| 赞皇县| 望谟县| 咸阳市|