如何使用angularJs
一、AngularJS入门之指令与表达式 AngularJS 通过 指令 扩展了 HTML,且通过 表达式 绑定数据到 HTML。 <!DOCTYPEhtml><html> <head> <metacharset="UTF-8"> <title>AngularJS入门</title> </head><bodyng-app=""ng-init="name=123"> <inputtype="text"id="input"ng-model="name"/> <divid="div"ng-bind="name+',Angular'">{{name}}</div> <inputtype="text"id="input2"ng-model="name"/> <p>我的第一个表达式:{{5+""+5+',Angular'}}</p> </body> <scriptsrc="libs/jquery-3.1.1.js"></script> <scriptsrc="libs/angular.js"></script> <scripttype="text/javascript">//varinput1=document.getElementById("input");//vardiv=document.getElementById("div");////input1.onkeyup=function(){//div.innerHTML=input1.value;//}//$("#input").keyup(function(){//$("#div").html($("input").val());//}); </script> </html> 二、AngularJS中的MVC与作用域 [MVC三层架构] 主要用于CRUD类应用,不适合游戏开发和DOM操作 创建一个Angular模块,即ng-app所绑定的部分,需传递两个参数: varapp=angular.module("myApp",[]);
app.controller("myCtrl",function($scope,$rootScope){ //$scope.name="name1"; $rootScope.age=14; $scope.classes={ name:"H51701",num:"33" }; }); app.controller("myCtrl1",function(){ }); 上图说明: 三、Angular过滤器 AngularJS中,过滤器可以使用一个管道字符(|)添加到表达式和指令中。 <!DOCTYPEhtml><html> <head> <metacharset="UTF-8"> <title></title> <linkrel="stylesheet"href="css/bootstrap.css"/> </head> <bodyng-app="app"ng-controller="ctrl"> <p>{{"aBcDeF"|uppercase}}</p> <p>{{"aBcDeF"|lowercase}}</p> <p>{{123456|currency}}</p> <formclass="form-horizontal"> </form> <divclass="form-group"> <label>请输入筛选信息:</label> <inputtype="text"ng-model="search"/> </div> <tableclass="tabletable-bordered"> <thead> <tr> <th>姓名</th> <th>年龄</th> <th>成绩</th> </tr> </thead> <trng-repeat="iteminclasses|filter:search|orderBy:'score'"> <td>{{item.name}}</td> <td>{{item.age}}</td> <td>{{item.score}}</td> </tr> </table> <p>{{"123456"|reverse}}</p> </body> <scriptsrc="libs/angular.js"></script> <script> angular.module("app",[]) .controller("ctrl",function($scope){ $scope.classes=[ {name:"张二",age:"12",score:"88"},{name:"张三",{name:"李四",age:"15",score:"78"},{name:"李五",{name:"王大麻子",age:"18",score:"98"},{name:"王二麻子",score:"98"} ]; })/* *自定义过滤器*/ .filter('reverse',function(){ returnfunction(text){if(!angular.isString(text)){return"您输入的内容不是字符串"; }else{returntext.split("").reverse().join(""); } } }) </script></html> 四、Angular服务Service 【服务Service】 <!DOCTYPEhtml><html> <head> <metacharset="UTF-8"> <title></title> </head> <bodyng-app="app"ng-controller="ctrl"> <pre>{{local}}</pre> <png-bind="myHeader"></p> <png-bind="num"></p> <p>{{gongneng}}</p> <p>将255转为16进制:{{hexafy}}</p> <p>{{123|filt}}</p> <p>{{123|filt1}}</p> </body> <scripttype="text/javascript"src="libs/angular.js"></script> <scripttype="text/javascript"> angular.module("app",$location,$timeout,$interval,$hexafy){ $scope.local=$location.absUrl(); $scope.myHeader="HelloWorld!"; $timeout(function(){ $scope.myHeader="Howareyoutoday?"; },2000); $scope.num=0; $interval(function(){ $scope.num++; },1000); $scope.gongneng=$hexafy.$$gongneng; $scope.hexafy=$hexafy.myFunc(255); })/*自定义服务*/ .service('$hexafy',function(){this.$$gongneng="将转入的数字,转为16进制";this.myFunc=function(x){returnx.toString(16); } }) .filter("filt",function(){returnfunction(x){returnx.toString(16); } })/*在过滤器中,调用自定义服务*/ .filter("filt1",function($hexafy){returnfunction(x){return$hexafy.myFunc(x); } }) </script></html> 【自定义服务factory】 <!DOCTYPEhtml><html> <head> <metacharset="UTF-8"> <title></title> <linkrel="stylesheet"href="libs/bootstrap.css"/> </head> <bodyng-app="app"ng-controller="ctrl"> <p> [功能]<br/> {{gongneng}}</p> <p> 255转成16进制为:{{num}}</p> </body> <scriptsrc="libs/angular.js"></script> <script> angular.module("app",[]) .config() .controller("ctrl",hexafy){ $scope.gongneng=hexafy.gongneng; $scope.num=hexafy.myFunc(255); }) .factory('hexafy',function(){varobj={ gongneng:"将转入的数字,转为16进制",myFunc:function(x){returnx.toString(16); } };returnobj; }) </script></html> 【自定义服务provide】 <!DOCTYPEhtml><html> <head> <metacharset="UTF-8"> <title></title> <linkrel="stylesheet"href="libs/bootstrap.css"/> </head> <bodyng-app="app"ng-controller="ctrl"> <p> [功能]<br/> {{gongneng}}</p> <p> 255转成16进制为:{{num}}</p> </body> <scriptsrc="libs/angular.js"></script> <script> angular.module("app",[])/*在配置阶段声明procide服务!*/ .controller("ctrl",hexafy){ $scope.gongneng=hexafy.gongneng; $scope.num=hexafy.myFunc(255); }) /*定义一个provider服务*/ .provider('hexafy',function(){//this.gongneng="将转入的数字,转为16进制"; this.$get=function(){varobj={ gongneng:"将转入的数字,转为16进制",myFunc:function(x){returnx.toString(16); } }returnobj; } }) </script></html> 五、Angular中的$http $http 是 AngularJS 中的一个核心服务,用于读取远程服务器的数据。 <!DOCTYPEhtml><html> <head> <metacharset="UTF-8"> <title></title> <linkrel="stylesheet"href="libs/bootstrap.css"/> </head> <bodyng-app="app"ng-controller="ctrl"> <!--<pre> {{data}} </pre>--> <divclass="container"style="margin-top:50px;"> <divclass="panelpanel-primary"> <divclass="panel-heading"> <divclass="panel-title"style="text-align:center;">H5-1701班级信息表</div> </div> <divclass="panel-body"> <tableclass="tabletable-bordered"> <thead> <tr> <th>姓名</th> <th>年龄</th> <th>爱好</th> <th>语文成绩</th> <th>数学成绩</th> <th>总分</th> </tr> </thead> <trng-repeat="itemindata|orderBy:'score.chinese+score.math'"> <tdng-bind="item.name"></td> <tdng-bind="item.age"></td> <tdng-bind="item.hobby"> <!--<spanng-repeat="ainitem.hobby">{{a}}</span>--> </td> <tdng-bind="item.score.chinese"></td> <tdng-bind="item.score.math"></td> <tdng-bind="item.score.chinese+item.score.math"></td> </tr> </table> </div> </div> </div> </body> <scriptsrc="libs/angular.js"></script> <script> angular.module("app",$http){ $http.post('h51701.json',{/*传递的参数对象*/}) .then(function(res){ $scope.data=res.data;//data从返回的信息中,取出需要的数据。为JSON对象(数组)}); }) </script></html> 六、Angular中的select 使用数组作为数据源。 <selectng-model="name"ng-options="x.siteforxinsites"> </select> 使用对象作为数据源. <!DOCTYPEhtml><html> <head> <metacharset="UTF-8"> <title></title> <linkrel="stylesheet"href="libs/bootstrap.css"/> <styletype="text/css"> *{ margin:10px; } </style> </head> <bodyng-app="app"ng-controller="ctrl"> <selectng-model="name"ng-options="xfor(x,y)insites"> </select> <divclass="alertalert-info"style="width:300px;"> 您选择的是:{{name}}</div> <tableclass="tabletable-bordered"> <tr> <th>序号</th> <th>姓名</th> </tr> <trng-repeat="iteminoptions"> <td>{{$index+1}}</td><!--$index表示遍历的索引,从0开始--> <td>{{item}}</td> </tr> </table> </body> <scriptsrc="libs/angular.js"></script> <script> angular.module("app",function($scope){ $scope.options=["张三","李四","王二麻子","杰小瑞"]; $scope.sites={ site01:"Google",site02:"Runoob",site03:"Taobao" }; }) </script></html> 七、Angular中的DOM与事件 ng-disabled="true/false"当传入true时,控件禁用。传入false是,启用; ng-show 默认隐藏 传入true时显示; ng-hide 默认显示 传入true是隐藏; ng-click定义了AngularJS中的点击事件。 <!DOCTYPEhtml><html> <head> <metacharset="UTF-8"> <title></title> <linkrel="stylesheet"href="libs/bootstrap.css"/> <styletype="text/css"> *{ margin:10px; } </style> </head> <bodyng-app="app"ng-controller="ctrl"> <inputtype="checkbox"ng-model="mySwitch">是否同意我是帅哥!</label> <buttonng-disabled="!mySwitch"class="btnbtn-primary">点我!</button> <p></p> <label> <inputtype="checkbox"ng-model="mySwitch1">是否显示?</label> <buttonng-show="mySwitch1"class="btnbtn-primary">点我!</button> <p></p> <label> <inputtype="checkbox"ng-model="mySwitch2">是否隐藏?</label> <buttonng-hide="mySwitch2"class="btnbtn-primary">点我!</button> <p></p> <buttonng-click="count=count+1">点我!</button> <p>{{count}}</p> <buttonng-click="func()">说一下感想吧!</button> </body> <scriptsrc="libs/angular.js"></script> <script> angular.module("app",$rootScope){ $scope.count=10; $scope.func=function(){ alert("我弹了一个窗!"); } }) </script></html> 八、Angular表单和输入验证 [AngularJS中的表单验证] <!DOCTYPEhtml><html> <head> <metacharset="UTF-8"> <title></title> <linkrel="stylesheet"href="libs/bootstrap.css"/> <styletype="text/css"> .row{ margin-bottom:10px; } .row.col-xs-5{ text-align:center; } .suc{ border-color:#3c763d; -webkit-box-shadow:inset01px1pxrgba(0,.075); box-shadow:inset01px1pxrgba(0,.075); } .suc:focus{ border-color:#2b542c; -webkit-box-shadow:inset01px1pxrgba(0,.075),006px#67b168; box-shadow:inset01px1pxrgba(0,006px#67b168; } .err{ border-color:#a94442; -webkit-box-shadow:inset01px1pxrgba(0,.075); } .err:focus{ border-color:#843534; -webkit-box-shadow:inset01px1pxrgba(0,006px#ce8483; box-shadow:inset01px1pxrgba(0,006px#ce8483; } </style> </head> <bodyng-app="app"ng-controller="ctrl"> <divclass="container"style="width:40%;margin:50pxauto;padding:0px;"> <divclass="panelpanel-primary"> <divclass="panel-heading"> <divclass="panel-title"style="text-align:center;"> 用户注册</div> </div> <divclass="panel-body"> <formaction=""method="get"class="form-horizontal"name="myForm"novalidate> <divclass="row"> <divclass="col-xs-3"> 用户名:</div> <divclass="col-xs-9"> <inputtype="text"class="form-control"ng-model="user.name"name="name"ng-minlength="4"ng-maxlength="10"requiredng-class="{suc:myForm.name.$valid&&myForm.name.$dirty,err:myForm.name.$invalid&&myForm.name.$dirty}"/> <pstyle="color:red;margin:0px;"ng-show="myForm.name.$invalid&&myForm.name.$dirty"> <!--当有填写记录且不合法时,p显示--> <spanng-show="myForm.name.$error.required">用户名必须填写!!!</span> <spanng-show="myForm.name.$error.minlength">用户名最少包含4个字符!!!</span> <spanng-show="myForm.name.$error.maxlength">用户名最多包含10个字符!!!</span> </p> </div> </div> <divclass="row"> <divclass="col-xs-3"> 邮箱:</div> <divclass="col-xs-9"> <inputtype="email"class="form-control"ng-model="user.mail"name="mail"requiredng-class="{suc:myForm.mail.$valid&&myForm.mail.$dirty,err:myForm.mail.$invalid&&myForm.mail.$dirty}"/> <pstyle="color:red;margin:0px;"ng-show="myForm.mail.$invalid&&myForm.mail.$dirty"> <!--当有填写记录且不合法时,p显示--> <spanng-show="myForm.mail.$error.required">邮箱必须填写!!!</span> <spanng-show="myForm.mail.$error.email">邮箱格式不合法!!!</span> </p> </div> </div> <divclass="row"> <divclass="col-xs-3"> 密码:</div> <divclass="col-xs-9"> <inputtype="password"class="form-control"ng-model="user.pwd"name="pwd"pattern="^w{6,18}$"requiredng-class="{suc:myForm.pwd.$valid&&myForm.pwd.$dirty,err:myForm.pwd.$invalid&&myForm.pwd.$dirty}"/> <pstyle="color:red;margin:0px;"ng-show="myForm.pwd.$invalid&&myForm.pwd.$dirty"> <!--当有填写记录且不合法时,p显示--> <spanng-show="myForm.pwd.$error.pattern">密码应为6-18位,且只能为字母、数字、下划线</span> </p> </div> </div> <divclass="row"> <divclass="col-xs-3"> 确认密码:</div> <divclass="col-xs-9"> <inputtype="password"class="form-control"ng-model="rePwd"name="rePwd"requiredng-class="{suc:myForm.rePwd.$dirty&&rePwd==user.pwd,err:myForm.rePwd.$dirty&&rePwd!=user.pwd}"/> <pstyle="color:red;margin:0px;"ng-show="myForm.rePwd.$dirty&&rePwd!=user.pwd"> <!--当有填写记录且不合法时,p显示--> 两次密码输入不一致!!!</p> </div> </div> <divclass="row"> <divclass="col-xs-5"> <inputtype="submit"value="注册"class="btnbtn-success"ng-disabled="myForm.$invalid||rePwd!=user.pwd"/> </div> <divclass="col-xs-5"> <inputtype="button"value="重置"class="btnbtn-warning"ng-click="resets()"/> </div> </div> </form> </div> </div> <pre> {{user.pwd}}</pre> </div> </body> <scriptsrc="libs/angular.js"></script> <script> $scope.resets=function(){ $scope.user=angular.copy($scope.initUser); } $scope.resets(); }) </script></html> (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |