首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将数据传递到基于组件的mat-stepper中的下一步

将数据传递到基于组件的mat-stepper中的下一步
EN

Stack Overflow用户
提问于 2020-03-03 21:22:55
回答 4查看 5.6K关注 0票数 2

我正在使用一个基于组件的mat stepper组件来显示一个线性过程。每个步骤都有自己的组件,如下所示

代码语言:javascript
复制
<mat-card>
    <mat-horizontal-stepper [linear]="isLinear" labelPosition="bottom" #stepper>
    
    <!-- Step-1 -->
    <mat-step [stepControl]="firstFormGroup">
       <ng-template matStepLabel>Select Items</ng-template>
       <select-item-component>
       <select-item-component>
       <div class="mt-5">
          <button mat-flat-button color="primary" matStepperNext>Next</button>
       </div>
    </mat-step>
    
    <!-- Step-2 -->
    <mat-step [stepControl]="firstFormGroup">
       <ng-template matStepLabel>Add Quantity</ng-template>
       <add-qty-component>
       <add-qty-component>
       <div class="mt-5">
          <button mat-flat-button color="primary" matStepperNext>Next</button>
       </div>
    </mat-step>
    
    <!-- Step-3 -->
    <mat-step [stepControl]="firstFormGroup">
       <ng-template matStepLabel>Conform</ng-template>
       <conform-step-component>
       <conform-step-component>
       <div class="mt-5">
          <button mat-flat-button color="primary" matStepperNext>Done</button>
       </div>
    </mat-step>
    </mat-horizontal-stepper>
 </mat-card>

步骤-1显示了多选项目列表,并将选定项目列表传递到下一步骤-2,并在步骤-2中添加每个项目的数量。

如何在下一次点击时将选中的项目从step-1传递到step-2,并在step-2中显示通过的项目以输入数量?

我已经创建了一个公共服务层来设置和获取选定的项目。在下一次单击之前,步骤2的组件的ngOnInit尝试从公共服务获取选定列表,但问题是组件2已启动。

在第1步中单击下一步后,是否可以初始化或重新初始化第二个组件?

从step-1移动到step-2后,如何显示step-2中的选中项列表?

对于上述场景,最佳方法是什么?

只要一个链接,任何参考,可以回答我的问题,这应该是足够的。

谢谢。

EN

回答 4

Stack Overflow用户

发布于 2020-03-09 23:37:29

要使组件输出值,请使用@Output

要使用组件外部的数据,请使用@Input

因为我不知道您的项目的类型,所以在本例中我将使用ItemType

选择-项目-组件

在select-item-component中,声明一个属性:

代码语言:javascript
复制
@Output() onDataChange: EventEmitter<ItemType> = new EventEmitter();

当select更改时,只需执行

代码语言:javascript
复制
this.onDataChange.emit(formValue);

add-qty-component

代码语言:javascript
复制
@Input() item: ItemType;

如果您想要在值更改时触发某些操作,请使用set。例如:

代码语言:javascript
复制
@Input() set item(value: ItemType) {
  this.loadOptions(value);
}

您可以在select-item-component中使用select1select2变量执行相同的操作。

父级

使用输出值和输入值。

代码语言:javascript
复制
...
<select-item-component (onDataChange)="select1 = $event"></select-item-component>
<select-item-component (onDataChange)="select2 = $event"></select-item-component>

...

<add-qty-component [item]="select1"></add-qty-component>
<add-qty-component [item]="select2"></add-qty-component>
...
票数 5
EN

Stack Overflow用户

发布于 2020-03-06 02:54:58

您可以组合使用@Output和@Input装饰器。

可以在子组件(stepper 1)中使用@Output修饰器将数据传输到其父组件。

@input装饰器可用于将数据从父组件发送到子组件(stepper 2)。

你可以阅读更多关于它的here

票数 4
EN

Stack Overflow用户

发布于 2020-03-06 09:45:15

您可以使用ControlContainer实现相同的目的,它允许您访问子组件和父组件中的表单控件

代码语言:javascript
复制
<button mat-raised-button (click)="isLinear = !isLinear" id="toggle-linear">
  {{!isLinear ? 'Enable linear mode' : 'Disable linear mode'}}
</button>
<mat-horizontal-stepper [linear]="isLinear" #stepper>
  <mat-step [stepControl]="firstFormGroup">
    <form [formGroup]="firstFormGroup">
      <ng-template matStepLabel>Fill out your name</ng-template>
     <step1></step1>
      <div>
        <button mat-button matStepperNext>Next</button>
      </div>
    </form>
  </mat-step>
  <mat-step [stepControl]="secondFormGroup">
    <form [formGroup]="secondFormGroup">
      <ng-template matStepLabel>Fill out your address</ng-template>
      <step2></step2>
      <div>
        <button mat-button matStepperPrevious>Back</button>
        <button mat-button matStepperNext>Next</button>
      </div>
    </form>
  </mat-step>
</mat-horizontal-stepper>

您的Step1组件应该如下所示

代码语言:javascript
复制
import {Component, OnInit} from '@angular/core';
import {ControlContainer, FormGroup, Validators} from '@angular/forms';


@Component({
  selector: 'step1',
  template:`
  <form [formGroup]="form"> 
   <mat-form-field>
        <mat-label>Name</mat-label>
        <input matInput placeholder="Last name, First name" formControlName="firstCtrl" required>
      </mat-form-field>
  </form>
  `
})
export class Step1 implements OnInit {
  isLinear = false;
  form: FormGroup;

  constructor(private controlContainer:ControlContainer) {}

  ngOnInit() {
   this.form=<FormGroup>this.controlContainer.control;
   this.form.valueChanges.subscribe(data=>console.log(data));
  }
}

您的Step2组件将如下所示

代码语言:javascript
复制
import {Component, OnInit} from '@angular/core';
import {ControlContainer, FormGroup, Validators} from '@angular/forms';

/**
 * @title Stepper overview
 */
@Component({
  selector: 'step2',
  template:`
  <form [formGroup]="form"> 
    <mat-form-field>
        <mat-label>Address</mat-label>
        <input matInput formControlName="secondCtrl" placeholder="Ex. 1 Main St, New York, NY"
               required>
      </mat-form-field>
  </form>
  `
})
export class Step2 implements OnInit {
  form: FormGroup;

  constructor(private controlContainer:ControlContainer) {}

  ngOnInit() {
   this.form=<FormGroup>this.controlContainer.control;
   this.form.valueChanges.subscribe(data=>console.log(data));
  }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60508242

复制
相关文章

相似问题

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