引言常用服務的使用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()
:用于改變網頁的url
replace()
:可以控制是否有返回鍵 url()
:改變主機的url
host()
方法的使用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>運行結果新聞熱點
疑難解答