標籤:style blog http io color ar os 使用 sp
AngularJS 常用功能
迭代輸出之ng-repeat標籤
ng-repeat讓table ul ol等標籤和js裡的數組完美結合
12345 |
<ul><li ng-repeat="person in persons">{{person.name}} is {{person.age}} years old.</li></ul> |
你甚至可以指定輸出的順序:
1 |
<li ng-repeat="person in persons | orderBy:‘name‘"> |
動態綁定之ng-model標籤
任何有使用者輸入,只要是有值的html標籤,都可以動態綁定js中的變數,
而且是動態綁定。
1 |
<input type="text" ng-model=‘password‘> |
對於綁定的變數,你可以使用{{}} 直接引用
1 |
<span>you input password is {{password}}</span> |
如果你熟悉fiter,你可以很容易的按你的需要格式輸出
1 |
<span>{{1288323623006 | date:‘yyyy-MM-dd HH:mm:ss Z‘}}</span> |
綁定點擊事件之ng-click事件
使用ng-click你可以很容易的為一個標籤綁定點擊事件。
1 |
<button ng-click="pressMe()"/> |
當然前提是你要在$scope域中定義的自己的pressMe方法。
和傳統的onclick方法不同,你甚至可以為ng-click方法傳遞一個對象,就像這樣:
12345 |
<ul><li ng-repeat="person in persons"><button ng-click="printf(person)"/></li></ul> |
當然還有ng-dblclick標籤
分支語句之ng-switch on、ng-if/ng-show/ng-hide/ng-disabled標籤
分支語句讓你在介面上都可以寫邏輯判斷。
1234567891011121314 |
<ul><li ng-repeat="person in persons"><span ng-switch on="person.sex"><span ng-switch-when="1">you are a boy</span><span ng-switch-when="2">you are a girl</span></span><span ng-if="person.sex==1">you may be a father</span><span ng-show="person.sex==2">you may be a mother</span><span>please input your baby‘s name:<input type="text" ng-disabled="!person.hasBaby"/></span><span></li></ul> |
校正文法之ng-trim ng-minlength ng-maxlength required ng-pattern 等標籤
表單中的輸入框,你可以使用上面的標籤來實現對使用者輸入的校正。
從字面意思上你已經知道了它們的意思。
123 |
<form name="yourForm"><input type="text" name="inputText" required ng-trim="true" ng-model="userNum" ng-pattern="/^[0-9]*[1-9][0-9]*$/" ng-maxlength="6" maxlength="6"/></form> |
你可以通過 $scope.yourForm.inputText.$error.required 來判斷輸入框是否為空白
你可以通過 $scope.yourForm.inputText.$invalid 來判斷輸入的內容是否滿足ng-pattern,ng-maxlength,maxlength
你通過$scope.userNum獲得的輸入內容是去掉前後空白的,因為ng-trim的存在。
下拉框之ng-options標籤
ng-options是為下拉框專門打造的標籤。
1 |
<select ng-model="yourSelected" ng-options="person.id as person.name in persons"></select> |
下拉框中顯示的是person.name,當你選中其中一個的時候,你可以通過yourSelected得到你選中的person.id.
控制css之ng-style標籤
ng-style幫你輕鬆控制你的css屬性
1 |
<span ng-style="myColor">your color</span> |
你可以通過給myColor賦值的形式來改變你想要的效果,就像這樣:
12 |
$scope.myColor={color:‘blue‘};$scope.myColor={cursor: ‘pointer‘,color:‘blue‘}; |
filter:
<!doctype html><html ng-app><head><meta http-equiv="Content-Type" content="text/html;charset=utf-8" /><script src="/js/staticsrc/jslib/angular.js"></script></head><body> <pre>過濾器功能點: currency 格式化數字為貨幣格式。 filter 從數組項中選擇一個子集。 lowercase 格式化字串為小寫。 orderBy 根據某個運算式排列數組。 uppercase 格式化字串為大寫。 </pre> <hr> <div ng-controller="personCtrl003"> <p>大寫-姓名為 {{ person.lastName | uppercase }}</p> <p>小寫-姓名為 {{ person.lastName | lowercase }}</p> </div> <hr> <div ng-init="quantity=1;price=5"> 數量:<input type="number" ng-model="quantity"> 價格:<input type="number" ng-model="price"> <p>貨幣格式-總價 = {{ (quantity * price) | currency }}</p> </div> <hr> <!--按照年齡從小到大: --> <div ng-controller="msgCtrl"> <li ng-repeat="msg in msgList | orderBy : ‘age‘"> {{(msg.name | uppercase) +‘, ‘+ msg.age}} </li> <hr> 輸入過濾(filter:name):<input type="text" ng-model="name"> <li ng-repeat="msg in msgList | filter:name | orderBy:‘age‘"> {{(msg.name | uppercase) +‘, ‘+ msg.age}} </li> </div> <script type="text/javascript"> function personCtrl003($scope){ $scope.person = { firstName: "John", lastName: "Doe", fullName: function() { var x; x = $scope.person; return x.firstName + " " + x.lastName; } }; }; function msgCtrl($scope){ $scope.msgList= [ {"name":"張三1","age":15}, {"name":"張三3","age":19}, {"name":"張三2","age":28}, {"name":"張三6","age":12} ]; } </script></body></html>
非同步請求之$http對象。
AngularJS 提供了一個類似jquery的$.ajax的對象,用於非同步請求。
在AngularJS中對非同步作業是推崇至極的,所以$http的操作都是非同步不像jquery.ajax裡還提供了async參數。
1234567 |
$http({method : ‘POST‘,params : { id:123}, data:{name:‘john‘,age:27}, url : "/mypath"}).success(function(response, status, headers, config){//do anything what you want;}).error(function(response, status, headers, config){//do anything what you want;}); |
如果你是POST請求,params裡的資料會幫你拼到url後面,data裡的資料會放到請求體中。
AngularJS常用功能