简体   繁体   English

Angular 4/5可观察-如何根据可观察属性更新组件模板?

[英]Angular 4/5 Observable - How to update component template depending on observable property?

Ok I am still a newbie. 好吧,我仍然是新手。 I have successfully created a 'dashboard' component that has a left sidebar with links. 我已经成功创建了一个“仪表板”组件,该组件的左侧边栏带有链接。 On the right is where I have content/components displayed that I want to change dynamically depending on what link was clicked on the left sidebar (see bootstrap sample of what this dashboard looks like here, click on the toggle button to view the sidebar: https://blackrockdigital.github.io/startbootstrap-simple-sidebar/ ). 右侧是我要显示的内容/组件的显示位置,我想根据左侧边栏上单击的链接来动态更改这些内容/组件(请参阅此仪表板外观的引导示例,单击切换按钮以查看边栏: https ://blackrockdigital.github.io/startbootstrap-simple-sidebar/ )。

I have created a DashboardService that has a Subject and an Observable to allow for sibling component communication. 我创建了一个DashboardService,它具有一个Subject和一个Observable来允许同级组件通信。 This works great since I have a console.log() that shows this communication working (when I click on link on sidebar in SidebarComponent, I console.log() a value 'emitted' by the DashboardService that is being listened to by the SidebarComponent's sibling, DashboardSectionComponent). 这很有用,因为我有一个console.log()来显示此通信的工作情况(当我单击SidebarComponent的侧栏上的链接时,我console.log()一个由DashboardService发射的值,该值由SidebarComponent的监听同级,DashboardSectionComponent)。

The problem that I am having is that the template in DashboardSectionComponent loads the correct component section ONLY on initial load of page - once I click on a link on the side bar the content is blank and nothing is rendered. 我遇到的问题是DashboardSectionComponent中的模板仅在页面的初始加载时才加载正确的组件部分-单击边栏上的链接后,内容为空白且什么都没有呈现。

Here is the service that allows the componenent communication: 这是允许组件通信的服务:

import { Injectable } from '@angular/core';
import { Subject } from 'rxjs/Subject';
import { Observable } from 'rxjs/Observable';

@Injectable()
export class DashboardService {

    private selectedComponentAlias = new Subject<string>();

    constructor() {}

    setSelectedComponentAlias(alias: string) {
        this.selectedComponentAlias.next(alias);
    }

    getSelectedComponentAlias(): Observable<string> {
        return this.selectedComponentAlias.asObservable();
    }

}

Here is the SidebarComponent: 这是SidebarComponent:

import { Component, OnInit } from '@angular/core';

import { DashboardService } from '../dashboard.service';

@Component({
    selector: 'app-sidebar',
    templateUrl: './sidebar.component.html',
    styleUrls: ['./sidebar.component.css']
})
export class SidebarComponent implements OnInit {

    constructor(private dashboardService: DashboardService) { }

    ngOnInit() {
    }

    onShowSection(event) {
        event.preventDefault();
        const componentAlias = event.target.getAttribute('data-componentAlias');
        this.dashboardService.setSelectedComponentAlias(componentAlias);
    }

}

here is the DashboardSectionComponent (the one that subscribes to the observable and I want to set property that controls the template views depending on the value that was caught) 这是DashboardSectionComponent(一个订阅了observable的对象,我想设置一个属性,该属性根据捕获的值来控制模板视图)

import { Component, OnInit, OnDestroy } from '@angular/core';
import { Subscription } from 'rxjs/Subscription';

import { DashboardService } from '../dashboard.service';

@Component({
    selector: 'app-dashboard-section',
    templateUrl: './dashboard-section.component.html',
    styleUrls: ['./dashboard-section.component.css']
})
export class DashboardSectionComponent implements OnInit, OnDestroy {

    private subscrition: Subscription;
    selectedComponentAlias: string = 'user-profile';

    constructor(private dashboardService: DashboardService) {

    }

    ngOnInit() {
        this.subscrition = this.dashboardService.getSelectedComponentAlias()
            .subscribe((selectedComponentAlias: string) => {
                this.selectedComponentAlias = selectedComponentAlias;
                console.log('user clicked: ',this.selectedComponentAlias);
            });
    }

    ngOnDestroy() {
        this.subscrition.unsubscribe();
    }

}

Finally here is the template for DashboardSectionComponent which might have wrong syntax: 最后,这是DashboardSectionComponent的模板,其语法可能错误:

<div *ngIf="selectedComponentAlias == 'my-cards'">
    <app-cards></app-cards>
</div>

<div *ngIf="selectedComponentAlias == 'user-profile'">
    <app-user-profile></app-user-profile>
</div>

<div *ngIf="selectedComponentAlias == 'user-settings'">
    <app-user-settings></app-user-settings>
</div>

Again, this works great (selectedComponentAlias is 'user-profile' on page load by default). 再次,这很好用(默认情况下,selectedComponentAlias是页面加载时的“用户配置文件”)。 But it goes blank after I click on a Sidebar link.... 但是,当我单击补充工具栏链接后,它变成空白。

Thanks. 谢谢。

这很容易-就像@RandyCasburn指出的那样,这是使路由正常工作的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将 observable 的值从服务传递到组件以更新模板 - How to pass value of observable from a service to component to update the template ObjectUnsubscribedErrorImpl 在 Angular 中模板中的 observable 上 - ObjectUnsubscribedErrorImpl on an observable in a template in Angular 如何过滤传递到角度分量的可观察对象? - How to filter an observable passed into an angular component? 如何在 Angular 组件中显示来自 Observable 的数据 - How to display data from Observable in Angular component 如何通过 observable 将数据从一个组件发送到另一个组件并在 Angular 的第二个 html 模板中显示数据 - How to send data from one component to another via observable and display the data in the secound html template in Angular 使用可观察的功能更新模板的元素 - Update an element of a template with a observable function 以角度将 Firebase observable 映射到模板 - Mapping an Firebase observable in angular to the template 角度-重用组件内可观察到的东西吗? - Angular - reusing observable within a component? 如何合并作为另一个observable的属性的observable - How to merge an observable that is a property of another observable 如何更新 aurealia 中的 observable - How to update an observable in aurealia
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM