如何使用angularJs(2)

<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <link href="https://www.jb51.net/css/bootstrap.css" /> </head> <body ng-app="app" ng-controller="ctrl"> <p>{{"aBcDeF"|uppercase}}</p> <p>{{"aBcDeF"|lowercase}}</p> <p>{{123456|currency}}</p> <form> </form> <div> <label>请输入筛选信息:</label> <input type="text" ng-model="search" /> </div> <table> <thead> <tr> <th>姓名</th> <th>年龄</th> <th>成绩</th> </tr> </thead> <tr ng-repeat="item in classes| filter:search | orderBy:'score'"> <td>{{item.name}}</td> <td>{{item.age}}</td> <td>{{item.score}}</td> </tr> </table> <p>{{"123456"|reverse}}</p> </body> <script src="https://www.jb51.net/libs/angular.js"></script> <script> angular.module("app",[]) .controller("ctrl",function($scope){ $scope.classes = [ {name:"张二",age:"12",score:"88"}, {name:"张三",age:"12",score:"88"}, {name:"李四",age:"15",score:"78"}, {name:"李五",age:"15",score:"78"}, {name:"王大麻子",age:"18",score:"98"}, {name:"王二麻子",age:"18",score:"98"} ]; }) /* * 自定义过滤器 */ .filter('reverse', function() { return function(text) { if(!angular.isString(text)){ return "您输入的内容不是字符串"; }else{ return text.split("").reverse().join(""); } } }) </script> </html>

四、Angular服务Service

【服务Service】

1、内置服务:

>>>使用内置服务必须在controlller中通过函数的参数注入进来!!!!

$location :返回当前页面的 URL 地址;

$http: 服务器请求数据,类似于AJax;

$timeout :对应了 JS window.setTimeout 函数。

$interval :对应了 JS window.setInterval 函数。

<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> </head> <body ng-app="app" ng-controller="ctrl"> <pre >{{local}}</pre> <p ng-bind="myHeader"></p> <p ng-bind="num"></p> <p >{{gongneng}}</p> <p>将255转为16进制:{{hexafy}}</p> <p>{{123|filt}}</p> <p>{{123|filt1}}</p> </body> <script type="text/javascript" src="https://www.jb51.net/libs/angular.js" ></script> <script type="text/javascript"> angular.module("app",[]) .controller("ctrl",function ($scope,$location,$timeout,$interval,$hexafy) { $scope.local=$location.absUrl(); $scope.myHeader = "Hello World!"; $timeout(function () { $scope.myHeader = "How are you today?"; }, 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) { return x.toString(16); } }) .filter("filt",function(){ return function(x){ return x.toString(16); } }) /*在过滤器中,调用自定义服务*/ .filter("filt1",function($hexafy){ return function(x){ return $hexafy.myFunc(x); } }) </script> </html>

【自定义服务factory】

factory 是一个函数用于返回值,通常我们使用 factory 函数来计算或返回值。(factory使用上,与service差距不大)

<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <link href="https://www.jb51.net/libs/bootstrap.css" /> </head> <body ng-app="app" ng-controller="ctrl"> <p> [功能]<br/> {{gongneng}} </p> <p> 255转成16进制为:{{num}} </p> </body> <script src="https://www.jb51.net/libs/angular.js"></script> <script> angular.module("app",[]) .config() .controller("ctrl",function($scope,hexafy){ $scope.gongneng = hexafy.gongneng; $scope.num = hexafy.myFunc(255); }) .factory('hexafy',function(){ var obj = { gongneng : "将转入的数字,转为16进制", myFunc:function(x){ return x.toString(16); } }; return obj; }) </script> </html>

【自定义服务provide】

1、在AngularJS中,Service,factory都是基于provider实现的。

2、在provider中,通过$get()方法提供了factory的写法,用于返回 value/service/factory。;

3、provider是三种自定义服务中,唯一可以写进config配置阶段的一种。

如果服务,必须要在配置阶段执行,那么必须使用provider。否则,一般使用Service或factory

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wyspgx.html