2017-08-03 47 views
0

我正在嘗試更改組件代碼中的角度材料進度欄值。我需要更改該值,而不使用綁定如何更改代碼中的角度材料2進度欄值

以下邏輯通常與htmlcontrols一起使用,如文本輸入。 但它不適用於進度條。

HTML:

<md-progress-bar mode="determinate" 
    id="progressBar" name="progressBar" #progressBar></md-progress-bar> 
<button (click)="test()">Test</button> 

代碼:

@ViewChild('progressBar') progressBar: any; 
test() { 
this.progressBar.value = "15"; 
} 
+0

爲什麼你需要更改,恕不使用綁定的值?你想實現什麼? –

+1

這是SignalR應用程序從服務器獲取百分比值。綁定在SignalR訂閱中不起作用。並且不僅用於進度條也用於輸入控件,但使用ViewChild訪問的邏輯與輸入控件一起工作。以某種方式不起作用的進度條。 –

+0

無論數據來自何處,綁定都應該有效。我的猜測是你有一個問題,因爲'this.progressBar.value'也應該工作。顯示真實的代碼,瞭解如何獲取數據以及何時何地需要更改值 – PierreDuc

回答

0

感謝您的changedetector溶液,據如我所述這與OnPush有關,現在它可以工作。但我不得不再次轉換爲使用綁定。所以基本上changedetector和binding在一起工作很好。

這是源代碼:

<md-progress-bar mode="determinate" [value]="percentage" style="width:70%;" 
id="progressBar" name="progressBar" #progressBar></md-progress-bar> 

    url: string; 
    lastSigResult: SigResult; 
    @ViewChild('lastSigResultSpan') lastSigResultSpan: any; 
    percentage:string = ""; 
    message:string = ""; 

    constructor(private changeDetector: ChangeDetectorRef,private route: ActivatedRoute, private router: Router, private signalRService: SignalRService) { 
     this.route.params.subscribe(
      params => { 
       console.log(params); 
       if (params['url']) { // Eğer url boş değilse 
        this.url = params['url']; 
       } 
      } 
     ); 

     this.subscribeSignalREvents(); 
    } 

    private subscribeSignalREvents(): void { 
     this.signalRService.connectionEstablished.subscribe(() => { 
      console.log("Connected to signalr..."); 
     }); 
     this.signalRService.messageReceived.subscribe((result: SigResult) => { 
      console.log(result); 
      // debugger; 
      this.changeDetector.markForCheck(); 
      this.lastSigResult = result; 
      this.lastSigResultSpan.nativeElement.value = result.Message; 
      this.message = result.Message; 
      this.percentage = result.Percentage; 
      this.changeDetector.detectChanges(); 
     }); 
    } 
0

值必須爲進度條的數:

this.progressBar.value = 15; 
+0

它不能正常工作,而且就我從文檔中看到的情況而言,它也允許像字符串一樣設置。 –