AngularJS Controller作用域
更新時間:2017年01月09日 08:51:34 作者:Zerone1993
這篇文章主要為大家詳細介紹了AngularJS Controller的作用域,具有一定的參考價值,感興趣的小伙伴們可以參考一下
$scope代表視圖與數(shù)據(jù)模型的中間橋梁:scope域中的對象都model和view都可以共享,并且數(shù)據(jù)是雙向同步controller的作用域:主要負責controller標簽包裹的元素的數(shù)據(jù)處理,如果子元素嵌套Controller,則相應的子元素的作用Controller以距離子元素最近的為準(這種最近作用的原則在Jmeter測試框架,avalon的ms-controller同樣體現(xiàn))
HTML正文:
<!-- 指定應用名及控制器 --> <body ng-app="myApp"> <div ng-controller="myCtrl01"> <p>myCtrl01的作用域</p> 名: <input type="text" ng-model="firstName"><br> 姓: <input type="text" ng-model="lastName"><br> <br> 姓名: {{firstName + " " + lastName}} <hr/> <p>myCtrl02的作用域</p> <div ng-controller="myCtrl02"> 名: <input type="text" ng-model="firstName"><br> 姓: <input type="text" ng-model="lastName"><br> <br> 姓名: {{firstName + " " + lastName}} </div> </div>
Javascript操作代碼:
//定義應用的控制器,負責具體數(shù)據(jù)處理: //定義angular應用的名稱:myApp:一個html中只有一個應用,如果存在多個以第一個為準 var app = angular.module('myApp', []); app.controller('myCtrl01', function($scope) { /* 后臺向scope域中存放對象:頁面存放的標簽: * ng-init 初始化變量 * ng-model:初始化變量并進行數(shù)綁定 */ $scope.firstName= "John"; $scope.lastName= "Doe"; }); app.controller('myCtrl02', function($scope) { $scope.firstName= "Hello"; $scope.lastName= "Python"; });
效果:
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
分享Angular http interceptors 攔截器使用(推薦)
AngularJS 是一個 JavaScript 框架。它可通過 <script> 標簽添加到 HTML 頁面。這篇文章主要介紹了分享Angular http interceptors 攔截器使用(推薦),需要的朋友可以參考下2019-11-11