abstract:1.Angular內(nèi)置serviceAngular為了方便開發(fā)者開發(fā),本身提供了非常多的內(nèi)置服務(wù)??梢酝ㄟ^https://docs.angularjs.org/api/ng/service查看AngularJS提供的內(nèi)置服務(wù)。在企業(yè)級(jí)開發(fā)中,常用的服務(wù)有以下這些:$cacheFactory 緩存服務(wù)$compile 編譯服務(wù)$filter 通過 $filter 服務(wù)可以格式化輸出數(shù)據(jù),也可以對(duì)數(shù)據(jù)
1.Angular內(nèi)置service
Angular為了方便開發(fā)者開發(fā),本身提供了非常多的內(nèi)置服務(wù)。可以通過https://docs.angularjs.org/api/ng/service查看AngularJS提供的內(nèi)置服務(wù)。在企業(yè)級(jí)開發(fā)中,常用的服務(wù)有以下這些:
$cacheFactory 緩存服務(wù)
$compile 編譯服務(wù)
$filter 通過 $filter 服務(wù)可以格式化輸出數(shù)據(jù),也可以對(duì)數(shù)據(jù)進(jìn)行過濾操作
$http AngularJS內(nèi)置的核心的服務(wù),主要和后臺(tái)請(qǐng)求相關(guān)
$location 基于window.location的Angular版本,功能更強(qiáng)大。比如路由地址的切換: $location.path('/home')
$log 開發(fā)過程中用到的多,輸入錯(cuò)誤和調(diào)試日志。和Chrome瀏覽器的console.log()、console.debug()等類似
$q 服務(wù)主要是用于異步函數(shù)返回一個(gè)promise,在路由中resovle屬性用的較多
$rootScope 一個(gè)應(yīng)用只有一個(gè) $rootScope,該服務(wù)可以用于每個(gè)頁(yè)面都需要使用的公共數(shù)據(jù)或者變量,但是開發(fā)過程中,建議盡量少用 $rootScope,調(diào)試起來不方便。因?yàn)樗且粋€(gè)全局變量。
2.Angular自定義Service
可以通過多種方式方式定義Service,常用的使用factory來定義一個(gè)service。代碼如下:
app.factory('dataService', function () { var appVerison = "1.0"; var showVersion = function () { return appVerison; }; return { appTitle: "Decorators Demo", showVersion: showVersion } });
3.在控制器之間共享數(shù)據(jù)使用Service
控制器和控制器之間共享數(shù)據(jù)也有多種方式,將變量或者函數(shù)綁定$rootScope是一種常見的方式,但是不推薦。常見的情況是使用Service來共享多個(gè)controller之間的數(shù)據(jù)。一個(gè)記錄圖書閱讀的系統(tǒng),需要記錄最后一次編輯的圖書信息。
在BooksController.js里面讀取currentUser服務(wù),在編輯的頁(yè)面給currentUser服務(wù)里面的lastBookEdited對(duì)象賦值。
定義currentUser服務(wù)
angular.module('app') .factory('currentUser', function () { var lastBookEdited = {}; return { lastBookEdited: lastBookEdited } });
在EditController.js
dataService.getBookByID($routeParams.bookId) .then(function (response) { vm.currentBook = response;//將當(dāng)前編輯的圖書對(duì)象賦值給lastBookEdited currentUser.lastBookEdited=vm.currentBook;屬性 }) .catch(function (response) { $log.error(response); });
BooksController.js
vm.currentUser=currentUser;
模板books.html
<div> {{books.summaryData.bookCount}} Books -- {{books.summaryData.readerCount}} Readers -- {{books.summaryData.grandTotalMinutes}} Total Minutes Read </div>
4.Decorators(修飾)在Angular Service的使用
在實(shí)際開發(fā)過程中,我們需要對(duì)自己的服務(wù)進(jìn)行增加一下方法,或者對(duì)引入的第三方服務(wù)增加一下方法,開發(fā)者可以不需要修改之前的源代碼,而是可以在運(yùn)行時(shí)為Service增加方法。這里需要用到Decorator-修飾。修飾模式是軟件設(shè)計(jì)里面一種經(jīng)典設(shè)計(jì)模式,在高級(jí)的面向?qū)ο笳Z(yǔ)言,比如Java、C#等都有實(shí)現(xiàn)。AngularJS代碼舉例:
var app = angular.module('app', []); app.controller('MainCtrl', function ($scope, dataService) { $scope.app = dataService; }); app.factory('dataService', function () { var appVerison = "1.0"; var showVersion = function () { return appVerison; }; return { appTitle: "Decorators Demo", showVersion: showVersion } }); app.config(function ($provide) { $provide.decorator('dataService', function ($delegate) { $delegate.sayHello = function () { return "a new function of 'dataService'"; }; return $delegate; }); });