[英]How Can I do Summary Components in Angular 2+
I am trying to create a component that will summarize content from other components, but i don't know how to properly do this.我正在尝试创建一个组件来汇总其他组件的内容,但我不知道如何正确执行此操作。
if there is a more proper way, please let me know rather than solving this one.如果有更合适的方法,请让我知道而不是解决这个问题。 TIA!
蒂亚!
For now, I'm trying to create this by passing the other components to the summary component through @Input, but it doesn't get passed, here is my code:现在,我试图通过通过@Input 将其他组件传递给摘要组件来创建它,但它没有通过,这是我的代码:
here is the application这是应用程序
stepper.html步进器.html
<mat-horizontal-stepper>
<mat-step [stepControl]="breakdownsGroup">
<form [formGroup]="breakdownsGroup">
<ng-template matStepLabel>Breakdowns</ng-template>
<app-breakdowns formArrayName="breakdowns"></app-breakdowns>
</form>
</mat-step>
<mat-step [stepControl]="summaryGroup">
<form [formGroup]="summaryGroup">
<ng-template matStepLabel>Summary</ng-template>
<app-summary [breakdowns]="breakdowns"></app-summary>
</form>
</mat-step>
</mat-horizontal-stepper>
stepper.ts stepper.ts
import { Component, OnInit } from '@angular/core';
import {FormBuilder, FormGroup, Validators, FormArray, FormControl} from '@angular/forms';
import { format } from 'url';
@Component({
selector: 'app-stepper',
templateUrl: './stepper.component.html',
styleUrls: ['./stepper.component.css']
})
export class StepperComponent implements OnInit {
isLinear = false;
remarksGroup: FormGroup;
breakdownsGroup: FormGroup;
summaryGroup: FormGroup;
constructor(private _formBuilder: FormBuilder) {
this.breakdownsGroup = this._formBuilder.group({
breakdowns: this._formBuilder.array([])
})
this.summaryGroup = this._formBuilder.group({
summary: this._formBuilder.array([])
})
}
ngOnInit() {
}
}
summary.ts摘要.ts
import { Component, OnInit, Input, OnChanges } from '@angular/core';
import { FormArray } from '@angular/forms';
@Component({
selector: 'app-summary',
templateUrl: './summary.component.html',
styleUrls: ['./summary.component.css']
})
export class SummaryComponent implements OnChanges {
@Input() remarks;
@Input() breakdowns;
ngOnChanges() {
}
showBreakdowns(){
console.log(this.breakdowns)
}
}
The problem here is in the line <app-summary [breakdowns]="breakdowns"></app-summary>
in stepper.html
.这里的问题在于
stepper.html
中的<app-summary [breakdowns]="breakdowns"></app-summary>
stepper.html
。 The way this is actually working is as follows:这实际工作的方式如下:
[ TS variable named 'breakdowns' in summary.ts ] = " TS variable named 'breakdowns' in stepper.ts " [ summary.ts 中名为'breakdowns' 的TS 变量] = " stepper.ts 中名为'breakdowns' 的TS 变量"
You might notice that there not actually a class-level variable called breakdown
in stepper.ts
.您可能会注意到
stepper.ts
实际上并没有一个称为breakdown
的类级变量。 There is however a variable breakdownsGroup
which contains within it a FormArray with the name breakdown
.然而,有一个变量
breakdownsGroup
,其中包含名称为breakdown
的 FormArray。 So really, in order to give stepper.html
something it recognises, what we want to pass into the right side of the above line is instead:所以真的,为了给
stepper.html
一些它可以识别的东西,我们想要传递到上面一行的右侧的是:
<app-summary [breakdowns]="breakdownsGroup?.controls?.breakdown?.value"></app-summary>
A more elegant way of achieving the same is to use an alias get
function in your typescript to reference the form control you are interested in. You can then use this alias to shorten the code in your HTML, like so:实现相同目的的更优雅的方法是在您的打字稿中使用别名
get
函数来引用您感兴趣的表单控件。然后您可以使用此别名来缩短 HTML 中的代码,如下所示:
stepper.ts stepper.ts
get breakdowns() {
return this.breakdownsGroup.get('breakdowns')
}
stepper.html步进器.html
<app-summary [breakdowns]="breakdowns?.value"></app-summary>
You should now be able to access the contents of the breakdowns
FormArray from within summary.ts
.您现在应该能够从
summary.ts
访问breakdowns
FormArray 的内容。
Note however, there is currently no logic in stepper.ts
which either assigns nor modifies breakdownsGroup
or the breakdowns
FormArray, therefore you will only have the initialised blank array passed into summary.ts
但是请注意,
stepper.ts
目前没有分配或修改breakdownsGroup
或breakdowns
FormArray 的逻辑,因此您只会将初始化的空白数组传递给summary.ts
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.