我創建了一個行爲主題,用於在應用程序中切換加載微調器圖標。Angular 2 - 具有行爲主體的多個參數?
服務:
// Observe our loader status
public loaderStatus: BehaviorSubject<boolean> = new BehaviorSubject<boolean>(false);
/**
* Toggle the loading indicator status
* @param value
*/
displayLoader(value: boolean) {
this.loaderStatus.next(value);
}
組件:
this._massEmpService.displayLoader(true); // Toggle true/false
HTML:
<div *ngIf="objLoaderStatus" class="loader" align="center">
<img src="images/loading-bars.svg" alt="" />
</div>
雖然這只是正常的一個單一的spinner實例,如果我想通過我的應用程序在多個區域使用此spinner,則該函數過於寬泛,如果同一頁面上存在多個spinner,則最終會觸發應用中的所有spinner實例。
我的問題:
是否有可能一個對象或多個PARAMS傳遞給行爲主體,這樣我不僅可以通過enabled/disabled
狀態,但也有一些類型的元素ID,所以我可以控制哪些微調我想展示。
實例目標:
<div *ngIf="objLoaderStatus && spinnerID == 'home'" class="loader" align="center">
<img src="images/loading-bars.svg" alt="" />
</div>
<div *ngIf="objLoaderStatus && spinnerID == 'search'" class="loader" align="center">
<img src="images/loading-bars.svg" alt="" />
</div>
函數調用:
this._massEmpService.displayLoader(true, 'search');
最新最好的方式去這樣做呢?我是否需要製作第二個行爲主題才能保留我想參考的微調器的elementID
?
感謝您的回覆。我沒有看到這與我所擁有的不同。他們有一個組件可以在頁面上包裝各個部分。該組件接收一個支持/禁用它的道具,這就是我對屬性和使用'ngIf'實質上做的事情。讓我困惑的部分是如何具體說明在處理加載spinners的多個實例時要切換哪一個。 – SBB
@SBB,它更多的是一個關注點的分離。服務具有全球範圍。只要你在整個頁面上使用微調器,它就很棒。如果你有多個帶有spinners的組件,每個組件都有一個結構指令是有意義的。當應用它的組件被隱藏時,它會自動隱藏/銷燬。指令中的elementRef將給出它所應用的元素。因此,如果需要,您可以獲取元素的所有dom屬性。您可以傳遞該條件以顯示微調器作爲指令的輸入。我會嘗試用例如 –
來更新我的答案。聽起來這可能適合我的情況,只是很難從鏈接的網站收集。如果時間允許,並且你有更好的例子,我會喜歡聽到它並接受它,因爲我的解決方案一旦實施。 – SBB