前 言 html
AngularJS 經過新的屬性和表達式擴展了 HTML。數組
使用起來很是方便。服務器
1. AngularJS的指令與表達式 |
2. AngularJS中的MVC與做用域 |
Model(模型層):應用程序中用於處理數據的部分。(包括將數據保存或修改到數據庫、變量、文件中)架構
3. AngularJS中的過濾器 |
angular.module("app",[]) .controller("ctrl",function($scope){ $scope.classes=[ {name:"張三",age:12,score:78}, {name:"李四",age:12,score:66}, {name:"二麻子",age:12,score:98}, {name:"小劉",age:12,score:54}, {name:"八萬",age:12,score:75}, ] }) /* * 自定義過濾器 */ .filter("showHello",function(){ return function(text){ return "Hello AngularJS"; } }) .filter("reverse",function(){ return function(text){ return text.split("").reverse().join(""); } }) /* * 自定義過濾器,同時須要傳遞過濾參數 * 調用過濾器示例:<p>{{12345678901| hideTel:4}}</p> * 傳入的參數4,將被過濾函數的num形參所接受 */ .filter("hideTel",function(){ return function(text,num){ num=num>0&&num<11?num:3; text=text+""; var newText=text.substring(0,11-num) +text.substring(11-num,11).replace(/\d/g,"*"); return newText; } }) /* * 自定義過濾器,實現根據姓名篩選數據的功能。 * >>> 調用示例: * 請輸入姓名:<input type="text" ng-model="name"/> * <tr ng-repeat="item in classes | filterByName:name"> */ .filter("filterByName",function(){ return function(items,search){ if(!search) return items; var arr=[]; for (var i=0;i<items.length;i++) { var index=items[i].name.indexOf(search); if (index>-1) { arr.push(items[i]); } } return arr; } })
4. AngularJS中的service & factory & provider |
4.1.1 內置服務:app
angular.module("app",[]) .controller("ctrl",function($scope,$location,$timeout,$interval,hexafy){ $scope.local = $location.$$host; $timeout(function(){ $scope.time="我是兩秒鐘以後出現!"; },2000); $scope.num=0; $interval(function(){ $scope.num++; },1000); $scope.gongneng=hexafy.gongneng; $scope.num1=hexafy.func(10); })
.service("hexafy",function(){ this.gongneng="將十進制數轉化爲16進制"; this.func=function(num){ return num.toString(16); } }) /*使用過濾器實現一樣功能*/ .filter("filter1",function(){ return function(num){ return num.toString(16); } }) /* 在過濾器中調用服務!! * 也必須在聲明過濾器的外層構造函數中,注入服務名稱!! */ .filter("filter2",function(hexafy,$location){ return function(num){ return hexafy.func(num); } })
angular.module("app",[]) .controller("ctrl",function($scope,hexafy){ $scope.gongneng=hexafy.gongneng; $scope.num1=hexafy.func(11); }) .factory("hexafy",function(){ var obj={ gongneng:"將十進制數轉化爲16進制", func:function(num){ return num.toString(16); } } return obj; })
.provider("hexafy",function(){ this.$get=function(){ var obj={ gongneng:"333" } return obj; } })
angular.module("app",[]) /*.config()表示配置階段,在聲明controller以前執行。能夠用於聲明一些在controller中 * 須要使用的全局變量、方法、服務等 */ .config(function($provide){ // 在配置階段聲明provider服務,須要在config中注入系統對象$provide $provide.provider("hexafy",function(){ this.$get=function(){ var obj={ gongneng:"444" } return obj; } }); }) .controller("ctrl",function($scope,hexafy){ $scope.gongneng=hexafy.gongneng; })
5. AngularJS中的select和表格 |
<select ng-model="site2" ng-options="item.site for item in sites"></select> <!-- 這種寫法,默認生成的option效果以下: <option value="http://www.google.com">Google</option> -->
<select ng-model="site3" ng-options="key for (key,value) in sitess"> <!-- <option value="value">key/value(取決for前面的內容)</option> --> </select>
<tr ng-repeat="item in options"> <!-- ng-repeat遍歷是,$index 表示當前的行索引! --> <td>{{$index + 1}}</td> <td>{{item}}</td> </tr>
6. AngularJS中的表單和輸入驗證 |
<div class="container" style="width: 500px;margin: 50px auto;padding: 0px;"> <div class="panel panel-primary"> <div class="panel-heading"> <div class="panel-title text-center"> 用戶註冊 </div> </div> <div class="panel-body"> <form class="form-horizontal" name="form" novalidate> <div class="row"> <div class="col-xs-3">用戶名</div> <div class="col-xs-9"> <input type="text"class="form-control"name="name" ng-model="user.name" ng-minlength="6" ng-maxlength="12"/> <p style="margin: 0px; color: red;" ng-show="form.name.$invalid && form.name.$dirty"> <span ng-show="form.name.$error.required">用戶名必須填寫</span> <span ng-show="form.name.$error.minlength">用戶名長度最小爲6位</span> <span ng-show="form.name.$error.maxlength">用戶名長度最大爲16位</span> </p> </div> </div> <div class="row"> <div class="col-xs-3">郵箱:</div> <div class="col-xs-9"> <input type="email" class="form-control" name="email" ng-model="user.email" required/> <p style="margin: 0px;color: red;" ng-show="form.email.$invalid && form.email.$dirty"> <span ng-show="form.email.$error.required">郵箱必須填寫</span> <span ng-show="form.email.$error.email">郵箱不合法</span> </p> </div> </div> <div class="row"> <div class="col-xs-3" >密碼:</div> <div class="col-xs-9"> <input type="password"class="form-control"name="pwd" ng-model="user.pwd" pattern="^\w{6,18}$" required/> <p style="margin: 0px; color: red;" ng-show="form.pwd.$invalid && form.pwd.$dirty"> <span ng-show="form.pwd.$error.pattern">密碼只能由6-18位的字母、數字、下劃線</span> </p> </div> </div> <div class="row"> <div class="col-xs-3" >確認密碼:</div> <div class="col-xs-9"> <input type="password"class="form-control" name="repwd" ng-model="user.repwd" required/> <p style="margin: 0px; color: red;" ng-show="user.pwd!=user.repwd && form.repwd.$dirty"> 兩次密碼輸入不一致 </p> </div> </div> <div class="row"> <div class="col-xs-5"> <input type="submit" value="註冊" class="btn btn-success" ng-disabled="form.$invalid||user.pwd!=user.repwd"/> </div> <div class="col-xs-5"> <input type="reset" value="重置" class="btn btn-warning"/> </div> </div> </form> </div> </div> </div>