Angular 根據(jù) service 的狀態(tài)更新 directive

字號:


    Angular JS (Angular.JS) 是一組用來開發(fā)Web頁面的框架、模板以及數(shù)據(jù)綁定和豐富UI組件。它支持整個開發(fā)進(jìn)程,提供web應(yīng)用的架構(gòu),無需進(jìn)行手工DOM操作。
    AngularJS是為了克服HTML在構(gòu)建應(yīng)用上的不足而設(shè)計的。HTML是一門很好的為靜態(tài)文本展示設(shè)計的聲明式語言,但要構(gòu)建WEB應(yīng)用的話它就顯得乏力了。這里AngularJS就應(yīng)運而生,彌補(bǔ)了HTML的天然缺陷,用于構(gòu)件Web應(yīng)用等。
    TL;DR
    這篇文章講解了三種根據(jù) service 的狀態(tài)更新 directive 的做法。分別是 $watch 表達(dá)式,事件傳遞,和 controller 的計算屬性。
    問題
    我有一個 readerService ,其中包含一些狀態(tài)信息(比如連接狀態(tài)和電量)。現(xiàn)在我需要做一個 directive 去展示這些狀態(tài)。因為它只需要從 readerService 中獲取數(shù)據(jù),不需要任何外部傳值,所以我直接把 service 注入進(jìn)去。但如何更新就成了一個問題。
    service 的代碼如下:
    const STATUS = {
    DETACH: 'DETACH',
    ATTACH: 'ATTACH',
    READY: 'READY'
    }
    class ReaderService {
    constructor() {
    this.STATUS = STATUS
    // The status will be changed by some callbacks
    this.status = STATUS.DETACH
    }
    }
    angular.module('app').service('readerService', readerService)
    directive 代碼如下:
    angular.module('app').directive('readerIndicator', (readerService) => {
    const STATUS = readerService.STATUS
    const STATUS_DISPLAY = {
    [STATUS.DETACH]: 'Disconnected',
    [STATUS.ATTACH]: 'Connecting...',
    [STATUS.READY]: 'Connected',
    }
    return {
    restrict: 'E',
    scope: {},
    template: `
    <div>
    {{statusDisplay}}
    </div>
    `,
    link(scope) {
    // Set and change scope.statusDisplay here
    }
    }
    })
    我嘗試過以下幾種辦法,下面一一介紹。
    方法一:$watch
    第一個想到的方法就是在 directive 中用 $watch 去監(jiān)視 readerService.status 。因為它不是 directive scope 的屬性,所以我們需要用一個函數(shù)來包裹它。Angular 會在 dirty-checking 時計算和比較新舊值,只有狀態(tài)真的發(fā)生了改變才會觸發(fā)回調(diào)。
    // In directive
    link(scope) {
    scope.$watch(() => readerService.status, (status) => {
    scope.statusDisplay = STATUS_DISPLAY[status]
    })
    }
    這個做法足夠簡單高效,只要涉及 readerService.status 改變的代碼會觸發(fā) dirty-checking ,directive 就會自動更新。service 不需要修改任何代碼。
    但如果有多個 directive 的屬性都受 service status 的影響,那 $watch 代碼就看得比較晦澀了。尤其是 $watch 修改的值會影響其他的值的時候。比如:
    // In directive
    link(scope) {
    scope.$watch(() => readerService.status, (status) => {
    scope.statusDisplay = STATUS_DISPLAY[status]
    scope.showBattery = status !== STATUS.DETACH
    })
    scope.$watch('showBattery', () => {
    // some other things depend on showBattery
    })
    }
    這種時候聲明式的編程風(fēng)格會更容易看懂,比如 Ember 或 Vue 里面的 computed property 。這個待會討論。
    方法二:$broadcast/$emit + $on
    這種思路是 service 每次狀態(tài)改變都發(fā)送一個事件,然后 directive 監(jiān)聽事件來改變狀態(tài)。因為 directive 渲染的時候也許 status 已經(jīng)更新了。所以我們需要在 link 中計算一個初始值。
    我最開始是用 $broadcast 去做的。代碼如下:
    // In service
    setStatus(value) {
    this.status = value
    // Need to inject $rootScope
    this.$rootScope.$broadcast('reader.statusChanged', this.status)
    }
    // In directive
    link(scope) {
    scope.statusDisplay = STATUS_DISPLAY[nfcReaderService.status]
    scope.$on('reader.statusChanged', (event, status) => {
    scope.statusDisplay = STATUS_DISPLAY[status]
    })
    }
    但馬上發(fā)現(xiàn) $broadcast 之后 UI 更新總要等 1 秒多(不過 $on 回調(diào)倒是很快)。Google 一番后知道原因是 $broadcast 是向下層所有 scope 廣播,廣播完成后再 dirty-checking 。一個更好的做法是使用 $emit ,它只會向上傳遞事件,不過不管發(fā)送事件還是監(jiān)聽事件都得用 $rootScope 。
    修改后的代碼如下:
    // In service
    setStatus(value) {
    this.status = value
    // Use $emit instead of $broadcast
    this.$rootScope.$emit('reader.statusChanged', this.status)
    }
    // In directive
    link(scope) {
    scope.statusDisplay = STATUS_DISPLAY[nfcReaderService.status]
    // Use $rootScope instead of scope
    $rootScope.$on('reader.statusChanged', (event, status) => {
    scope.statusDisplay = STATUS_DISPLAY[status]
    })
    }
    如果因為某些原因不得不用 $broadcast 的話,你可以在 $on 回調(diào)最后用 $digest 或 $apply 強(qiáng)制觸發(fā) dirty-checking ,這也可以達(dá)到快速更新 UI 的目的。
    方法三:controller + property
    我個人覺得前兩個方法能解決問題,但代碼維護(hù)性都不太好。 $watch 在屬性相互關(guān)聯(lián)的情況下非常難看懂, $emit/$on 需要把一些邏輯寫兩次(初始化 directive 時和回調(diào)執(zhí)行時)。方法一中我提到了有些時候聲明式的屬性比 $watch 更容易看懂。這個方法就是使用 controller 。directive 可以設(shè)置自己的 controller 作為數(shù)據(jù)來源(或者說 view model),我們可以把那些需要計算的屬性作為 controller 的屬性。這樣 dirty-checking 時它們就會自動計算。
    // In directive
    class ReaderController {
    constructor($scope, readerService) {
    this.readerService = readerService
    }
    get statusDisplay() {
    return STATUS_DISPLAY[this.readerService.status]
    }
    }
    return {
    // ...
    controller: ReaderController,
    controllerAs: 'vm',
    template: `
    <div>
    {{vm.statusDisplay}}
    </div>
    }
    這樣一來,大部分邏輯都可以挪到 controller 中。如果沒有 DOM 操作我們甚至可以不寫 link 方法。也沒必要加入額外的 $watch 和 $on 。只是因為 dirty-checking 的特性,綁定到 template 的屬性往往會多計算幾次。所以屬性必須非常簡單。大部分情況下這不會有什么問題。
    以上內(nèi)容是小編給大家介紹的Angular 根據(jù) service 的狀態(tài)更新 directive,希望對大家有所幫助!