angularJS(三)内置服务
来源:互联网 发布:开淘宝网店要多钱 编辑:程序博客网 时间:2024/06/06 01:37
服务
服务是一个对象或函数,对外提供特定的功能。
1.内置服务
1、$location是对原生Javascript中location对象属性和方法的封装。
app.controller('demoController',['$scope','$location',function($scope,$location){ //绝对路径 $scope.absUrl = $location.absUrl(); //返回url协议 $scope.protocol = $location.protocol(); //返回url服务器使用的端口号 $scope.port = $location.port(); //当前路径 $scope.path = $location.path(); //返回一个url的锚部分:第一个#后面的内容 $scope.hash = $location.hash(); //返回url的查询字符串部分 $scope.search = $location.search();}]);
2、timeout&interval对原生Javascript中的setTimeout和setInterval进行了封装。
app.controller('demoController',['$scope','$timeout','interval',function($scope,$timeout,$interval){ $timeout(function(){ $scope.time = new Date();},2000); $interval(function(){ $scope.time = new Date();},1000);}]);
3、$log打印调试信息
app.controller('demoController',['$scope','$log',function($scope,$log){ $log.log('打印信息'); $log.info('普通信息'); $log.warn('警告信息'); $log.error('错误信息'); $log.debug('调试信息');}]);
4.
//$http本质是对XMLHttperRequest对象封装app.controller('demoController',['$scope','$http',function($scope,$http){ $http({ url:'example.php',//请求地址 method:'post',//请求方式也可以为get params:{//会被转换成查询字符串追加在URL后面。如果值不是字符串,会被JSON序列化。 'name':'ari' //参数会转为?name=ari的形式 }, data:{name:'itcast',age:10},//post传参 header:{//请求头信息 'Content-Type':'application/x-www-form-urlencoded' } }).success(function(data,status,headers,config){ //data响应体 status 相应的状态值 headers是头信息的getter函数 ,可以结束一个参数,用来获取对应名字值 config 是用来生成原始请求的完整设置对象。 }).error(function(data,status,headers,config){ //失败回掉 });}]);
2.自定义服务
通过上面例子得知,所谓服务是将一些通用性的功能逻辑进行封装方便使用,AngularJS允许将自定义服务。
1、factory方法
App.factory('showTime', ['$filter', function ($filter) { var now = new Date(); now = $filter('date')(now,'yyyy/MM/dd'); return now;}]);App.controller('DemoController', ['$scope', 'showTime', function($scope, showTime) { $scope.now = showTime.now;}]);
2、service方法
App.service('showTime', ['$filter', function ($filter) { var now = new Date(); this.now = $filter('date')(now,'yyyy/MM/dd');}]);App.controller('DemoController', ['$scope', 'showTime', function($scope, showTime) { $scope.now = showTime.now;}]);
3、value方法定义常量 在介绍服务时曾提到服务本质就是一个对象或函数,所以自定义服务就是要返回一个对象或函数以供使用。
app.value('author','itcast');app.controller('DemoController', ['$scope', 'atuhor',function($scope,author){ $scope.author = author;}]);
阅读全文
0 0
- angularJS(三)内置服务
- AngularJs内置服务$http
- AngularJS 内置服务:$http
- AngularJS内置服务
- AngularJS 内置服务
- angularjs的内置服务
- 前端MVC学习总结(三)——AngularJS服务、路由、内置API、jQueryLite
- AngularJS 别有洞天的内置服务
- AngularJS 内置服务 ~ $http().then()
- AngularJS 内置服务 ~ $timeout设置定时器(只调用一次)
- AngularJS自定义服务与内置服务
- AngularJS内置的一些服务之$http
- AngularJS的内置服务与路由
- AngularJS的内置服务和路由
- AngularJs的内置服务$window,$document,$location
- AngularJS 内置服务 ~ $interval:设置时钟
- Node.js开发入门(九)——使用AngularJS内置服务
- AngularJS内置服务$http的使用(对数据库的增删改)
- 《Processing编程学习指南》第1章 像素
- 原生短信添加一个自动亮屏的功能(不包括彩信)
- VueScan Pro(扫描仪驱动程序)官方中文去水印破解版64位V9.5.92下载 | 内置vuescan 注册码
- 简单瀑布流小插件(宽度一定)
- Android安卓——数据存储之文件存储
- angularJS(三)内置服务
- 递归的理解
- 遍历List,Set的方法
- 第一节点习题
- fork之写时拷贝技术
- java 动态代理和静态代理浅析
- PatchMatch
- MyBatis的动态sql_set与if结合的动态更新
- 【java学习记】—勤快式、懒汉式单例