【发布时间】:2019-01-13 22:19:47
【问题描述】:
我正在尝试在我的 UI 中启用功能,该功能将在选择/取消选择时动态显示选择。
import { Wizard } from './report-common';
import { Router } from '@angular/router';
import { DataService } from './../shared/service/data.service';
import { TreeNode } from './../shared/dto/TreeNode';
import { Component, OnInit } from '@angular/core';
import { Subject } from 'rxjs/Subject';
import 'rxjs/Rx';
import * as STEPS from '../shared/constants';
import html from './report-builder.component.html';
import css from './report-builder.component.css';
@Component({
selector: 'report-builder',
template: html,
providers: [DataService],
styles: [css]
})
export class ReportBuilderComponent implements OnInit {
selectedProductLine: TreeNode<string>[];
rightside: Wizard = new Wizard([STEPS.PRODUCT_LINE]);
productLineSubject = new Subject<TreeNode<string>[]>();
//this allows the html to access the constants
HTML_STEPS = STEPS;
constructor (private dataService: DataService, private router: Router) {}
ngOnInit() {
this.productLineSubject.subscribe((productline) => this.productLineChange(productline));
}
public productLineChange(productLine: TreeNode<string>[]):void {
this.selectedProductLine = productLine;
this.rightside.setSelection(this.extractDisplayNames(productLine), STEPS.PRODUCT_LINE);
}
private extractDisplayNames <T>(nodes: TreeNode<T>[]): string[] {
return nodes.map(node => node.displayName);
}
}
html相关代码:
<div *ngFor="let step of rightside.steps">
<li *ngIf="!step.hidden">
<rightside-component class="side-button" [selectionSubject]="step.selections">
</rightside-component>
</li>
</div>
“向导”结构如下:(report-common.ts)
import { DataService } from './../shared/service/data.service';
import { TreeNode } from './../shared/dto/TreeNode';
import { BehaviorSubject } from 'rxjs/BehaviorSubject';
export class WizardStep {
selections: BehaviorSubject<any[]> = new BehaviorSubject<any[]>([]);
}
export class Wizard {
currentComponent:WizardStep;
steps:WizardStep[];
public setSelection(selections:any[], component:string) {
let componentStep = this.steps.find(step => step.component === component);
if(!componentStep) { return; }
componentStep.selections.next(selections);
}
}
右侧组件.ts:
export class RightSideComponent implements OnInit {
selections: string[];
@Input() selectionSubject: BehaviorSubject<string[]>;
constructor(private cd: ChangeDetectorRef) {}
ngOnInit() {
this.selectionSubject.subscribe((selections) => {
this.selections = selections;
this.cd.detectChanges();
});
}
}
Rightside.component.html:
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<div>
<ul class="selection-list">
<li *ngFor="let item of selections">
<button class="btn">
<i class="fa fa-close">
{{item}}
</i>
</button>
</li>
</ul>
</div>
在这里,我在列表中的每个项目前面添加了一个删除图标。每当单击按钮时,该项目不仅应该从列表中消失,而且应该从改变它的原始结构中取消选择。
我在这里尝试使用 changedetection,但没有按预期工作。
我基本上想使用 Angular 5 和我的数据结构做类似于 http://next.plnkr.co/edit/1Fr83XHkY0bWd9IzOwuT?p=preview&utm_source=legacy&utm_medium=worker&utm_campaign=next&preview 的事情。任何关于如何从这一点开始的想法将不胜感激。如果需要任何其他代码,请告诉我。
【问题讨论】:
标签: html angular typescript data-binding angular5