【问题标题】:Angular Material Stepper - Calling next() not working until I click on the viewAngular Material Stepper - 在我点击视图之前调用 next() 不起作用
【发布时间】:2019-03-07 00:55:50
【问题描述】:

我正在用 angular 7 和 angular material 构建一个电子应用程序

我有一个 Stepper,第二步调用 electron main 以使用户选择将保存应用程序内容的文件夹。 当它被选中时,它会调用“selectedWorkingFolder”,它将步骤设置为已完成,应该使用 (this.stepper.next()) 直接转到步骤 3,除非我单击窗口上的任何位置,否则这不起作用。

这是一个展示它的 gif 图像

https://i.gyazo.com/7e17510822bc7b3946bc6e917e965466.mp4

这是控制器代码

import { Component, OnInit, Input, ChangeDetectorRef, ViewChild } from '@angular/core';
import { ElectronService } from 'src/app/services/electron/electron.service';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';


@Component({
    selector: 'app-main',
    templateUrl: './main.component.html',
    styleUrls: ['./main.component.css']
})

export class MainComponent implements OnInit {

    @ViewChild('stepper') stepper;

    firstFormGroup: FormGroup;
    secondFormGroup: FormGroup;

    isLinear = true;

    constructor(
        private readonly ipcServ: ElectronService,
        private cdRef: ChangeDetectorRef,
        private _formBuilder: FormBuilder,
    ) {
        this.firstFormGroup = this._formBuilder.group({
            firstCtrl: ['', Validators.required]
        });

        this.secondFormGroup = this._formBuilder.group({
            secondCtrl: ['', Validators.required]
        });

        this.ipcServ.on('databaseCheckResult', (event, docs) => {
            console.log(docs);
            this.changeState(docs);
        });

        this.ipcServ.on('selectedWorkingFolder', (event, docs) => {
            this.stepper.selected.completed = true;
            this.stepper.selected.editable = false;
            this.stepper.next();
        });

    }

    ngOnInit() {
        this.ipcServ.send('checkdb');
    }

    changeState(action) {
        if (action === 'unconfigured') {
            this.cdRef.detectChanges();
        } else {

        }
    }

    stepperEvents(event) {
        if (event.selectedIndex === 1) {
            this.ipcServ.send('selectFolder');
        }

    }
}

这是html代码

<mat-vertical-stepper labelPosition="bottom" #stepper (selectionChange)="stepperEvents($event)">
    <mat-step [stepControl]="firstFormGroup">
        <form [formGroup]="firstFormGroup">
            <ng-template matStepLabel>Enter your profile name</ng-template>
            <mat-form-field>
                <input matInput placeholder="Profile Name" formControlName="firstCtrl" required>
            </mat-form-field>

            <br>
        </form>
    </mat-step>
    <mat-step [stepControl]="secondFormGroup">
        <ng-template matStepLabel>Select the working folder</ng-template>
    </mat-step>
    <mat-step>
        <ng-template matStepLabel>Done</ng-template>
        You are now done.
    </mat-step>
</mat-vertical-stepper>

而且我对 Angular 还是很陌生,所以任何关于如何改进我的代码的提示都会有很大帮助。

谢谢

【问题讨论】:

    标签: angular angular-material


    【解决方案1】:

    我想您已修复它,但我认为问题在于您调用了 Angular 之外的全局回调,因此更改检测未运行。要告诉 Angular 你准备好了,请将你的代码包装到 ngZone.run()

    constructor(private ngZone: NgZone) {...}
    
    this.ipcServ.on('selectedWorkingFolder', (event, docs) => {
      this.ngZone.run(() => {
        this.stepper.selected.completed = true;
        this.stepper.selected.editable = false;
        this.stepper.next();
      });           
    });
    
    

    【讨论】:

    • 是的,这就是修复。
    • 遇到了同样的问题。这让我发疯了..我尝试了 1001 个其他选项,因为它是一个复杂的部分,并认为它是不同的东西,但没有成功.. 将我的代码包装在 ngZone 中并完美运行.. 愚蠢.. 谢谢你的好先生!
    猜你喜欢
    • 1970-01-01
    • 2022-08-05
    • 2019-04-18
    • 1970-01-01
    • 2022-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-29
    相关资源
    最近更新 更多