angularjs隨筆01 資料雙向繫結理解 自訂過濾器 時鐘更新列子

來源:互聯網
上載者:User

標籤:輸出   簡寫   存在   time   過濾器   小數點   meta   oda   .json   

    1.   資料的雙向繫結可以簡單的理解為,無論在文字框中輸入什麼,都會在資料模型中顯示出來輸入的內容,

雙向繫結的模型和資料是進行動態綁定的,即時檢查進行修改。

<input type="text" ng-model="name">{{name}}

      在上述代碼中,通過angular.js將資料模型對象($scope)的name屬性與文本模型繫結在一起,然後就實現了在輸入框輸入什麼都會

在文本模型中顯示對應的內容,即時更新。

      控制器controller,

   2.時鐘更新列子

  index.html

 1 <!doctype html> 2 <html > 3 <head> 4     <meta charset="utf-8"> 5 </head> 6 <script src="js/angular-1.3.0.js"></script> 7 <script src="js/clock.js"></script> 8 <body ng-app="myApp"> 9 <div ng-controller="MyController">10     <h1>{{clock}}</h1>11 </div>12 </body>13 </html>

clock.js

      

 1 var app=angular.module("myApp",[]); 2 app.controller("MyController",function($scope){ 3         var updateClock=function(){ 4            $scope.clock=new Date(); 5         }; 6         setInterVal(function(){ 7            $scope.$apply(updateClock); 8          },1000); 9           updateClock();10 });11 //不理解$apply時12 function MyController($scope, $timeout) {13           var updateClock = function() {14            $scope.clock = new Date();15            $timeout(function() {16                   updateClock();17                  }, 1000);18             };19             updateClock();20 };

   如果在頁面中給ng-app賦值,就要在js中對其進行聲明

 var app=angular.module("myApp",[]);

3.自訂過濾器
angularjs中存在多種過濾:
1.currency 將數值格式化,轉化為貨幣格式
2.uppercase 字元轉換為大寫 lowercase 字元轉換為小寫
3.number number:2 就代表小數點後兩位
4.date格式化 下面是angularjs教程中總結的date格式化
 1 {{ today | date:‘medium‘ }} <!-- Aug 09, 2013 12:09:02 PM --> 2       {{ today | date:‘short‘ }} <!-- 8/9/1312:09PM --> 3       {{ today | date:‘fullDate‘ }} <!-- Thursday, August 09, 2013 --> 4       {{ today | date:‘longDate‘ }} <!-- August 09, 2013 --> 5       {{ today | date:‘mediumDate‘ }}<!-- Aug 09, 2013 --> 6       {{ today | date:‘shortDate‘ }} <!-- 8/9/13 --> 7       {{ today | date:‘mediumTime‘ }}<!-- 12:09:02 PM --> 8       {{ today | date:‘shortTime‘ }} <!-- 12:09 PM --> 9       ? 年份格式化10       四位年份: {{ today | date:‘yyyy‘ }} <!-- 2013 -->11       兩位年份: {{ today | date:‘yy‘ }} <!-- 13 -->12       一位年份: {{ today | date:‘y‘ }} <!-- 2013 -->13       ? 月份格式化14       英文月份: {{ today | date:‘MMMM‘ }} <!-- August -->15       英文月份簡寫: {{ today | date:‘MMM‘ }} <!-- Aug -->16       數字月份: {{ today |date:‘MM‘ }} <!-- 08 -->17       一年中的第幾個月份: {{ today |date:‘M‘ }} <!-- 8 -->18       ? 日期格式化19       數字日期: {{ today|date:‘dd‘ }} <!-- 09 -->20       一個月中的第幾天: {{ today | date:‘d‘ }} <!-- 9 -->21       英文星期: {{ today | date:‘EEEE‘ }} <!-- Thursday -->22       英文星期簡寫: {{ today | date:‘EEE‘ }} <!-- Thu -->23       ? 小時格式化24       24小時制數字小時: {{today|date:‘HH‘}} <!--00-->25       一天中的第幾個小時: {{today|date:‘H‘}} <!--0-->26       12小時制數字小時: {{today|date:‘hh‘}} <!--12-->27       上午或下午的第幾個小時: {{today|date:‘h‘}} <!--12-->28       ? 分鐘格式化29       數字分鐘數: {{ today | date:‘mm‘ }} <!-- 09 -->30       一個小時中的第幾分鐘: {{ today | date:‘m‘ }} <!-- 9 -->31       ? 秒數格式化32       數字秒數: {{ today | date:‘ss‘ }} <!-- 02 -->33       一分鐘內的第幾秒: {{ today | date:‘s‘ }} <!-- 2 -->34       毫秒數: {{ today | date:‘.sss‘ }} <!-- .995 -->35       ? 字元格式設定化36       上下午標識: {{ today | date:‘a‘ }} <!-- AM -->37       四位時區標識: {{ today | date:‘Z‘ }} <!--- 0700 -->
      5.filter過濾器   將包含傳入的元素的對象輸出,拋棄不包含的 
6.json將一個JSON或者對象轉換成字串
7.limitTo 根據傳入的數字截取該數字長度的字串,為正值時從頭部截取,為負值的時候從尾部截取
8.orderBy 根據傳入的欄位,按照此欄位進行排序

angularjs隨筆01 資料雙向繫結理解 自訂過濾器 時鐘更新列子

聯繫我們

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