引言常用服務的使用1scope和rootScope服務的使用11 scope服務的使用12 rootscope服務的使用13 全局服務注入到其他方法中apPRun方法2 http服務的使用21 httpget方法的使用22 httppost方法的使用3 location服務的使用31 host方法的使用32 url方法的使用33 path方法的使用34 replace方法的使用注意和path的區別replace方法是不可以后退的path是可以后退的4 cacheFactory服務的使用5 timeout服務和interval服務的使用6 sce服務的使用61 sce服務的使用
在本博客中主要介紹一下服務的使用,AngularJS已經給我們提供的服務,AngularJS給我們提供的服務主要分為兩大類:
provider供應商的服務不具有provider供應商的服務那么這兩種服務有什么區別呢?具有供應商的服務可以通過config方法配置我們的服務屬性,不具有供應商的服務是不可以通過config配置屬性的。還有的服務的作用域是不一樣的。舉個例子:比如$scope服務,作用域是控制器范圍內的,所以$scope服務只能注入到控制器中,還有的服務是全局范圍的,可以注入到更多的方法中,比如$filter服務。接下來我們就來介紹幾個比較常用的服務,其中有:$scope,$rootScope,$http,$location,$cacheFactory,$timeout,$interval,$sce。
$scope和$rootScope服務的使用 在最初使用控制器的時候,我們第一次接觸$scope服務,我們可以使用$scope服務給某一個控制器設置變量,也就是說我們使用$scope設置的變量是局部的,是屬于控制器范圍的,如果我們想設置全局范圍的變量,那么我們可以使用$rootScope服務,其次$scope是沒有供應商的,但是$rootScope是具有供應商的,也就是說$scope服務僅僅可以注入到我們的控制器中,但是$rootScope服務是可以注入到多種方法當中。接下來我們就用實例來看一下這兩個服務的具體使用。
$scope服務的使用$scope注入到控制器中) var app=angular.module("myApp",[]); app.controller("firstController", function ($scope) { $scope.name="hello world" })創建我們的html片段<body> <div ng-controller="firstController">{{name}}</div></body>運行結果
$rootscope服務的使用 使用$rootscope服務我們可以設置我們的全部變量,$rootscope同樣可以注冊到我們的 控制器中,但是和$scope相比而言,$rootscope服務所創造的變量不是局限于控制器作用域中。
name屬性是在控制器中,一個name屬性沒有在控制器作用域)<body> <div ng-controller="firstController">{{name}}</div> {{name}}</body>運行結果
app.run方法) 在前面我們說過,服務分為兩種,服務的作用域是不一樣的,一些全局的作用域服務可以通過run 方法初始化全局的數據 ,只對全局作用域起作用 如$rootScope服務,
run方法注入全局變量 var app=angular.module("myApp",[]); app.run(function($rootScope){ $rootScope.name="hello $rootScope"; })html片段<body> {{name}}</body>運行結果
$scope為例,我們看一看(錯誤代碼) var app=angular.module("myApp",[]); app.run(function($scope){ $scope.name="hello $rootScope"; })程序錯誤:找不到供應商
$http服務的使用 關于$http服務的使用主要用于Ajax的實現,其中$http服務主要有三種方法get,post,jsonp。
get:用于get請求post:用于post請求jsonp:用于jsonp請求在這里我們主要演示一下get方法和post方法的使用。
person.json)[{ "name":"wpx", "age":"20"},{ "name":"zlr", "age":"22"}]$http.get方法的使用get方法發送請求(get方法的第一個參數是url,第二個參數是向服務器傳的參數) //控制器 var app=angular.module("myApp",[]); app.controller("firstController",function($scope,$http){ $scope.get=function(){ $http.get("person.json",{params:{name:"wpx"}}).then(function (result) { console.log(result.data) }) } }) //html標簽 <div ng-controller="firstController"> <button ng-click="get();">發送請求</button> </div>使用參數列表發送get請求(params會生成到url后面,get請求) var app=angular.module("myApp",[]); app.controller("firstController",function($scope,$http){ $scope.get=function(){ $http({ url:"person.json", params:{ id:10 }, method : 'GET', }).then(function (result) { console.log(result.data) }) } }) //html標簽 <div ng-controller="firstController"> <button ng-click="get();">發送請求</button> </div>代碼運行發送的url
$http.post方法的使用post方法發送請求(post方法的第一個參數是url,第二個參數是向服務器傳的參數) var app=angular.module("myApp",[]); app.controller("firstController",function($scope,$http){ $scope.get=function(){ $http.post("person.json",{ "id":"wpx" }).then(function (result) { console.log(result.data) }) } }) //html標簽 <div ng-controller="firstController"> <button ng-click="get();">發送請求</button> </div>使用參數列表發送POST請求(注意:請求體用的data參數) var app=angular.module("myApp",[]); app.controller("firstController",function($scope,$http){ $scope.get=function(){ $http({ url:"person.json", method:"POST", data:{ id:"wpx" } }).then(function (result) { console.log(result.data) }) } }) //html標簽 <div ng-controller="firstController"> <button ng-click="get();">發送請求</button> </div>使用該方法發送的url
$location服務的使用 $location服務解析地址欄中的 URL(基于 window.location),讓你在應用代碼中 能獲取到。改變地址欄中的 URL 會反應$location 服務中,反之亦然。在這里主要介紹$location服務的幾種方法。
host():返回url中的主機路徑path():用于改變網頁的urlreplace():可以控制是否有返回鍵 url():改變主機的urlhost()方法的使用
url方法的使用#!/hello)

path方法的使用url結果添加了#!/hello)

replace方法的使用(注意和path的區別,replace方法是不可以后退的,path是可以后退的)

$cacheFactory服務的使用 關于$cacheFactory服務的使用,是緩存的實現,一般用到該服務可以實現多個控制器之間的數據共享,比如在控制器A中存入緩存,然后在控制器B中獲得緩存的數據,接下來我們就來看一下這個功能怎么實現。

$timeout服務和$interval服務的使用 關于$timeout服務和$interval服務的使用類似于js中的代碼setTimeout()和setInterval()方法類似,$timeout服務是一定時間之后執行一次代碼,$interval服務是一定時間間隔之后執行一次代碼,接下來我們看一下這兩個服務如何使用。

$sce服務的使用 在前面我們使用過ngSanitize插件,$sce服務和ngSanitize插件的作用差不多,該服務也用于解析html的,接下來我們就來使用一下該服務。
$sce服務的使用* 創建我們的控制器
var app=angular.module("myApp",[]); app.controller("firstController",function($scope,$sce){ $scope.text=$sce.trustAsHtml("<h1>AAAA</h1>") })創建html片段 <div ng-controller="firstController"> <div ng-bind-html="text"></div> </div>運行結果
新聞熱點
疑難解答