简体   繁体   English

我如何在 Angular 2+ 中做摘要组件

[英]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目前没有分配或修改breakdownsGroupbreakdowns FormArray 的逻辑,因此您只会将初始化的空白数组传递给summary.ts

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

相关问题 如何链接到Angular 2+中两个组件的路线? - How can I link to a route to two components in Angular 2+? Angular(2+):当所有子组件都已初始化时,我如何从父组件知道? - Angular (2+) : how can i know from parent component when all child-components were initialized? 如何在Angular 2+中显示AngularJS - How do I display AngularJS in Angular 2+ 如何在Angular 2+中基于组件创建模板? - How to create templates based in components in Angular 2+? 如何清理 Angular 2+ 项目,以便可以将其压缩并通过电子邮件发送 - How do I clean an angular 2+ project so it can be zipped and emailed 在Angular 2+中继承组件 - Inheriting components in Angular 2+ 如何在Angular 2+单元测试中触发键盘事件? - How do I trigger a keyboard event in an Angular 2+ unit test? 如何在Angular 2+中将Observable内部的值作为函数返回 - How do I return the value inside of an Observable as the function in Angular 2+ Angular 2+,有什么办法可以在模块中声明所有组件,然后导入app.module.ts? - Angular 2+, Is there any way I can declare all the components in a module and then import in app.module.ts? 如何更改Angular 2+的剑道迷你图的条形颜色 - How can I change the bar color of a kendo sparkline for Angular 2+
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM