AngularJs學習筆記4——四大特性之雙向資料繫結

來源:互聯網
上載者:User

標籤:script   module   內容   char   model   bsp   console   angular   即時   

雙向資料繫結

方向1:模型資料(model)綁定到視圖(view)

             實現方法:①.{{model變數名}}  ②.常用指令(ng-repeat)

方向2:將視圖(view)中使用者輸入的資料繫結到模型資料(model)

             實現方法:ng-model指令,用在表單組件中(input select)

                              $watch()監聽模型變數值的改變—>執行指定的方法

                              $watch(‘變數名’,function(){…});

 

一個很簡單的栗子:

  即時擷取輸入框中輸入的內容,列印在控制台

  

 1 <!DOCTYPE html> 2 <html ng-app="myApp"> 3 <head lang="en"> 4   <meta charset="UTF-8"> 5   <title>雙向資料繫結</title> 6   <script src="js/angular.js"></script> 7 </head> 8 <body ng-controller="myCtrl"> 9 <!--通過ng-model把視圖資料繫結到模型上,同時又通過{{}}的形式把模型資料繫結到視圖上,實現雙向資料繫結-->10 <input type="text" ng-model="kw"/>11 <p>{{kw}}</p>12 <script>13   var app = angular.module(‘myApp‘,[‘ng‘]);14   app.controller(‘myCtrl‘, function ($scope) {15     $scope.$watch(‘kw‘,function(){16       console.log($scope.kw);17     })18 19   });20 </script>21 </body>22 </html>

 

AngularJs學習筆記4——四大特性之雙向資料繫結

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.