分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 网页技术

angularjs学习笔记--服务

发布时间:2023-09-06 01:06责任编辑:蔡小小关键词:js


?在angularjs中,服务是一个函数或对象,可在angularjs应用中使用。其中$location服务可以返回当前页面的url地址。要使用它,需要在controller中进行定义,作为一个参数传入到controller中。

<!DOCTYPE html><html lang="en"><head><script src="../angular/angular.js"></script></head><body><p ng-app="myApp" ng-controller="customersCtrl">{{myUrl}}</p><script>var app = angular.module(‘myApp‘,[]);app.controller(‘customersCtrl‘,function($scope,$location){$scope.myUrl = $location.absUrl();});</script></body></html>

  

$http服务向服务器发送请求,应用响应服务器传送过来的数据。

var app = angular.module("myApp",[]);app.controller("myCtrl",function($scope,$http){$http.get("welcome.html").then(function(response){$scope.myWelcome = response.data;});});

  

$timeout

var app = angular.module("myApp",[]);app.controller("myCtrl",function($scope,$timeout){$scope.myHeader = "hello world";$timeout(function(){$scope.myHeader = "how are you today?";},2000);});

  

创建自定义服务:可以创建自定义服务,并链接到自己的模块中,但在访问自定义服务时,需要在定义控制器的时候独立添加,设置依赖关系。当自定义服务连接到自己的应用上后,可以在控制器/指令/过滤器或其他服务中使用它。

app.service(‘hexafy‘,function(){this.myFunc = function(x){return x.toString(16);}});app.controller(‘myCtrl‘,function($scope,hexafy){$scope.hex = hexafy.myFunc(255);});app.filter(‘myFormat‘,[‘hexafy‘,function(hexafy){return function(x){return hexafy.myFunc(x);};}]);

  

未完待续。。。

angularjs学习笔记--服务

原文地址:http://www.cnblogs.com/haimengqingyuan/p/7442079.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved