2017-09-27 78 views
11

是否可以在 stepper內重置(或只是跳轉到第一步)? 它沒有記錄在文檔中,但可以這樣做嗎? 在文檔中指出,步進器是建立在「CDK步進器」(link?)上,但我找不到任何導致我實現目標的示例。角度4&材料步進器

+0

@Edric又怎會讓卸下'角material2'標籤? \ o/ – Faisal

+0

@Faisal只是想知道,但是[tag:angular-material]和[tag:angular-material2]之間是否有區別? – Edric

+0

沒有imo,但大多數用戶使用'angular-material2'標籤發佈問題。我認爲我們應該創建一個這個標籤作爲'angular-material'的同義詞。一旦我在該標籤中有足夠的分數,我會建議。 – Faisal

回答

14

好吧,似乎我找到了一個解決方案(但它沒有在API的任何地方聲明)。

  1. 添加對步進器的引用,然後在組件typescript文件中添加引用ViewChild
  2. 創建一個方法來更改步進器的索引。

HTML:

<mat-horizontal-stepper [linear]="true" #stepper> 
    <!-- Content here --> 
</mat-horizontal-stepper> 

TS:

import { Component, ViewChild } from '@angular/core'; 
@Component({ 
    // ... 
}) 
export class AppComponent { 
    @ViewChild('stepper') stepper; 

    /** 
    * Changes the step to the index specified 
    * @param {number} index The index of the step 
    */ 
    changeStep(index: number) { 
     this.stepper.selectedIndex = index; 
    } 
} 
+0

這似乎太好了,以至於沒有。在使用此類時出現錯誤:'TypeError:無法讀取MdVerticalStepper.webpackJsonp中未定義的 屬性'toArray'.../../../cdk/esm5/stepper.es5.js.CdkStepper._anyControlsInvalid(stepper .es5.js:351)'有人知道爲什麼嗎? –

0

檢查如果這個stepper component可以解決您的問題。

+2

謝謝!但我沒有要求替代品,我不想「污染」與未知來源的項目 –

+0

我不知道你是什麼意思污染。這是純粹用角和角材料建造的。 – bhanu

7

It is possible to jump to a specific stepper. MatStepper exposes a public property selectedIndex which specifies the current selected step index. It can be set. The index starts at 0.

在模板:

添加ID到您的步進例如#stepper。然後在您的步驟是在功能添加一個按鈕,並通過步進編號上(click)象下面這樣:

<mat-horizontal-stepper [linear]="isLinear" #stepper> 
    <!-- Your other steps here --> 
    <mat-step [stepControl]="secondFormGroup"> 
     <!-- Content in the step --> 
     <div> 
      <!-- Other actions here -->     
      <button mat-button (click)="resetStepper(stepper)">Reset</button> 
     </div> 
    </mat-step> 
    <!-- More steps here --> 
</mat-horizontal-stepper> 

..並在您的打字稿:

import { MatStepper } from '@angular/material'; 

resetStepper(stepper: MdStepper){ 
    stepper.selectedIndex = 0; 
} 

Stackblitz demo

+1

43分鐘太晚:)謝謝無論如何,它總是很好有一個像你的演示解決方案的工作演示 –

+0

只是看到了問題:)不要忘記upvote,如果這是有用的:) – Faisal

+0

好吧,「重置」似乎工作只是部分,如果你想重置表單,它會清除值,但表單保持無效。 我分叉您的演示在這裏演示演示(https://stackblitz.com/edit/angular-material2-beta-2upkqd?file=app/app.component.ts) –

-1

我嘗試提供的解決方案,他們幾乎爲我工作,我得到了以下錯誤:

TypeError: Cannot read property 'toArray' of undefined 
    at MdVerticalStepper.webpackJsonp.../../../cdk/esm5/stepper.es5.js.CdkStepper._anyControlsInvalid (stepper.es5.js:351) 

這個小調整工作對我來說(使用材料2公測11):

changeStep(index: number) { 
    console.log(this.stepper); 
    this.stepper._selectedIndex = 2; 
} 
+0

錯誤:'Property'_selectedIndex'是私有的,只能在類' CdkStepper'.' – jadianes

+0

和?你如何初始化你的步進機?喜歡這個? '@ViewChild('wizardStepper')公共步進器;'和在這樣的html中:'' –

+0

我試過'@ViewChild('stepper')stepper'和'@ViewChild('stepper')步進器:MatStepper'; – jadianes