首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >动态组件

动态组件
EN

Stack Overflow用户
提问于 2016-09-05 19:28:23
回答 1查看 176关注 0票数 1

在视图中动态显示组件的正确方式是什么?

例如,我有一个具有导航和视图的组件。单击导航中的按钮不同的子组件应该会在视图中出现/消失。每个子组件都属于不同的类型。

代码语言:javascript
运行
复制
import { Component, Input } from '@angular/core';
import { Controls } from './controls.service';

@Component({
  selector: 'Controlpanel',
  templateUrl: 'app/templates/controlPanel.component.html',
  directives: Controls.directives()
})
export class ControlPanelComponent {
  controls = Controls.objects;
  activeControl:string = Controls.objects[0].name;
}

--

代码语言:javascript
运行
复制
<nav class="container-fluid">
    <div class="row">
            <a *ngFor="let control of controls" (click)="toggleControl(control.name, $event)" role="button" href="#" class=" text-uppercase"> 
                {{control.name}}
            </a>
    </div>
</nav>
<div>
    <this is where i want the sub-component to appear/>
</div>
EN

回答 1

Stack Overflow用户

发布于 2016-09-05 19:39:48

你可以添加你想要加载到窗体上的每个控件,然后给每个控件添加一个*ngIf,但我不会这么做。

添加要加载指令的位置。并创建一个childRoute来显示您想要显示的组件(指令):

代码语言:javascript
运行
复制
<nav class="container-fluid">
    <div class="row">
            <a *ngFor="let control of controls" (click)="toggleControl(control.name, $event)" role="button" href="#" class=" text-uppercase"> 
                {{control.name}}
            </a>
    </div>
</nav>
<div>
      <!-- this is where you show your sub-component t by routing -->
     <router-outlet></router-outlet>

    </div>
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39329624

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档