AngularJS 內建指令(二)

來源:互聯網
上載者:User

標籤:

下面將要介紹的指令會以父級範圍為原型產生子範圍。這種繼承的機制可以建立一個隔

離層,用來將需要協同工作的方法和資料模型對象放置在一起。

ng-app

 

和ng-controller是特殊的指令,因為它們會修改嵌套在它們內部的指令的範圍。

ng-app

 

為AngularJS應用建立$rootScope,ng-controller則會以$rootScope或另外一個

ng-controller

 

的範圍為原型建立新的子範圍。

1. 子範圍:

<!doctype html><html ng-app="myApp"><head>  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.0-rc.3/angular.js"></script></head><body>  <div ng-controller="SomeCtrl">    {{ someProperty }}    <button ng-click="someAction()">Communicate</button>  </div>  <script>    angular.module(‘myApp‘, [])    .controller(‘SomeCtrl‘, function($scope) {      $scope.someProperty = ‘hello computer‘;      $scope.someAction = function() {        $scope.someProperty = ‘hello human‘;      };    });  </script></body></html>

 

內建指令

 

ng-controller的作用是為嵌套在其中的指令建立一個子範圍,避免將所有操作

和

 

模型都定義在$rootScope上。用這個指令可以在一個DOM元素上放置控制器。

ng-controller

 

接受一個參數expression,這個參數是必需的。

expression

 

參數是一個AngularJS運算式。

子

 

$scope只是一個JavaScript對象,其中含有從父級$scope中通過原型繼承得到的方法和屬

性,包括應用的

 

$rootScope。

嵌套在

 

ng-controller中的指令有訪問新子$scope的許可權,但是要牢記每個指令都應該遵守

的和範圍相關的規則。

<!doctype html><html ng-app="myApp"><head>    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.0-rc.3/angular.js"></script></head><body>    <div ng-controller="SomeCtrl">        {{ someModel.someProperty }}        <button ng-click="someAction()">Communicate</button>    </div>    <script>        angular.module(‘myApp‘, [])        .controller(‘SomeCtrl‘, function ($scope) {            // create a model            $scope.someModel = {                // with a property                someProperty: ‘hello computer‘            }            // set methods on the $scope itself            $scope.someAction = function () {                $scope.someModel.someProperty = ‘hello human‘;            };        });    </script></body></html>

 多個控制器:

 1 <!doctype html> 2 <html ng-app="myApp"> 3 <head> 4     <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.0-rc.3/angular.js"></script> 5 </head> 6 <body> 7  8     <div ng-controller="SomeCtrl"> 9         {{ someBareValue }}10         <button ng-click="someAction()">Communicate to child</button>11         <div ng-controller="ChildCtrl">12             {{ someBareValue }}13             <button ng-click="childAction()">Communicate to parent</button>14         </div>15     </div>16 17     <script>18         angular.module(‘myApp‘, [])19         .controller(‘SomeCtrl‘, function ($scope) {20             // anti-pattern, bare value21             $scope.someBareValue = ‘hello computer‘;22             // set actions on $scope itself, this is okay23             $scope.someAction = function () {24                 $scope.someBareValue = ‘hello human, from parent‘;25             };26         })27         .controller(‘ChildCtrl‘, function ($scope) {28             $scope.childAction = function () {29                 $scope.someBareValue = ‘hello human, from child‘;30             };31         });32     </script>33 34 </body>35 </html>
View Code


嵌套控制器,會同步修改:

<!doctype html><html ng-app="myApp"><head>    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.0-rc.3/angular.js"></script></head><body>    <div ng-controller="SomeCtrl">        {{ someModel.someValue }}        <button ng-click="someAction()">Communicate to child</button>        <div ng-controller="ChildCtrl">            {{ someModel.someValue }}            <button ng-click="childAction()">Communicate to parent</button>        </div>    </div>    <script>        angular.module(‘myApp‘, [])        .controller(‘SomeCtrl‘, function ($scope) {            // best practice, always use a model            $scope.someModel = {                someValue: ‘hello computer‘            }            $scope.someAction = function () {                $scope.someModel.someValue = ‘hello human, from parent‘;            };        })        .controller(‘ChildCtrl‘, function ($scope) {            $scope.childAction = function () {                $scope.someModel.someValue = ‘hello human, from child‘;            };        });    </script></body></html>

 

AngularJS 內建指令(二)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.