我有不同的标签,显示带组件。我需要做的是让这些组件在项目未加载(未定义)时隐藏,并显示下拉列表中的任何项目何时加载。为此,我需要从项目id或名称中获取值,然后可以显示其他带状组件。
如您所见,这是包含组件的选项卡之一:
<div class="tab tab-style">
<ribbon-item style="width:10%;" [title]="'Load/Save Project'">
<project></project>
</ribbon-item>
<div class="ribbon-divider"></div>
<ribbon-item style="width:12%;" [title]="'Project settings'">
<project-settings></project-settings>
</ribbon-item>
<div class="ribbon-divider"></div>
<ribbon-item style="width:23%;" [title]="'Environment'">
<environment></environment>
</ribbon-item>
<div class="ribbon-divider"></div>
<ribbon-item class="pd-width" [title]="'Project dates'">
<project-dates></project-dates>
</ribbon-item>
<div class="ribbon-divider"></div>
这是包含project.id的该选项卡中的项目组件:
<div class="button-wrapper">
<select #projectSelect class="custom-select custom-select-project" (change)="loadProject(projectSelect.value)">
<option selected disabled>Open projects</option>
<option *ngFor="let project of projects" [value]=project.id>{{project.name}}</option>
</select>
<button class="btn-main" (click)="createNewProject()">New project</button>
<button class="btn-main">Save project</button>
<button class="btn-main">Save as</button>
基本问题是,如何根据id值或数据形式(除了project.component)设置组件的其余部分以显示该组件?我可以使用ngIf或类似的东西吗?谢谢你的帮助
发布于 2017-09-15 17:02:51
花了一些时间为输入和输出的想法(角度使用的双向数据绑定)、嵌套组件以及“转换”(在另一个组件的内容区域中包含一个组件)做了一个非常基本的布局。
那是这里。
亮点与pezetter所描述的完全相同:在发出值的子组件中,您需要使用EventEmitter和Output
// SelectorComponent
@Output() valueSelected: EventEmitter<number> = new EventEmitter();
selectValue(val: number) {
this.valueSelected.emit(val);
}它将在您的(change)组件的<select>事件上调用。
// SelectorComponent
<select #selectorElem (change)="selectValue(selectorElem.value)">然后,父程序监视发出的事件。
// ParentComponent (template)
<selector design="purple" (valueSelected)="valueChanges($event)"></selector>
// ParentComponent
selectedVal: string;
valueChanges(val: number) {
this.selectedVal = val;
}最后,您只需要根据输出的值(传递给父组件)来隐藏或显示组件。在本文中,有两种不同的方法可以完成:组件本身(alt-child)上的*ngIf:
// ParentComponent
<alt-child design="gray" *ngIf="selectedVal">或者组件中的*ngIf (child)
// ParentComponent
<child design="red" [value]="selectedVal" [position]="2">
// ChildComponent
<div [ngClass]="design" *ngIf="value >= position">这当然是更深入的理解,但希望一些人可以发现,在周围玩的敲击有用。
发布于 2017-09-15 15:55:15
将@Output添加到项目组件中。然后,发出一个包含项目值的事件,该事件将启用具有ngIf或类似内容的其他组件。
在project.component.ts中:
@Output() setProjectValue = new EventEmitter();
loadProject(val){
let foundProject;
//Load project logic here...
foundProject == ...; // once its done:
this.setProjectValue.emit(foundProject); //This will output to your parent component.
}在您的app.component.html (或项目组件的父组件)中:
<project (setProjectValue)="project = $event;"></project> //I think you're allowed to bind like this.但是,我建议只使用服务/提供者来存储加载的项目。每次加载项目时设置该值,并在该父组件中使用一个条件,该条件检查服务是否具有已加载的项目值。
发布于 2017-09-15 16:51:20
您可以考虑为选项卡用户界面使用路由而不是嵌套组件。
然后HTML看起来如下所示:
<div class="panel-body">
<div class="wizard">
<a [routerLink]="['info']" routerLinkActive="active">
Basic Information <span [ngClass]="{'glyphicon glyphicon-exclamation-sign':
!isValid('info')}"></span>
</a>
<a [routerLink]="['tags']" routerLinkActive="active">
Search Tags <span [ngClass]="{'glyphicon glyphicon-exclamation-sign':
!isValid('tags')}"></span>
</a>
</div>
<router-outlet></router-outlet>
</div>基本上有标签和路由器插座。
然后,您可以使用路由参数轻松地将数据传递到不同的路由。
我在这里有一个完整的例子: APM-Final文件夹中的https://github.com/DeborahK/Angular-Routing。
https://stackoverflow.com/questions/46242568
复制相似问题