2017-05-08 381 views
6

我試圖設置#name1的值,如下所示。但它顯示編譯時錯誤,如下所示。請你告訴我如何設置值text組件?在這裏,我使用單向數據綁定和模板驅動的方法。類型'ElementRef'上不存在屬性「值」

[ts]屬性'值'在類型'ElementRef'上不存在。

的.html

<ion-input type="text" name="{{question?.name}}" #name1="ngModel" ngModel> </ion-input> 

.TS

@ViewChild('name1') name1: ElementRef; 

    constructor(){ 

    } 

getAnswer(){ 
    this.name1.value = 'Hello';//here it shows the above error 
    } 
+0

以下是ElementRef的文檔:https://angular.io/docs/ts/latest/api/core/index/ElementRef-class.html。但是你錯過了ngModel的全部觀點,那就是能夠在視圖和組件狀態之間進行雙向綁定。 –

+0

@ Mr.Sampath ...我面臨着類似的錯誤..請問你可以看看這個鏈接.... https://stackoverflow.com/questions/49044826/property-value-does-not-exist-on類型元素 – Heena

+0

@ Mr.Sampath ...我面臨着類似的錯誤..請問你可以看看這個鏈接.... https://stackoverflow.com/questions/49044826/property-value-does-not -exist-on-type-element – Heena

回答

8

使用的組件類型,而不是可變的模板

@ViewChild(TextInput) name1: TextInput; 

這也可能工作(我不知道Ionic)。它可以與原生HTML輸入元素一起工作,但如果它是一個Angular組件,以上是首選方式。

this.name1.nativeElement.value = 'Hello'; 
+1

This is not working'this.name1.nativeElement.value ='Hello';'。但是第一個工作正常。請給我們分享一篇關於這個概念的好文章嗎?非常感謝你:) – Sampath

+1

我並沒有真正期待第二個例子的工作。它可以使用本地HTML輸入元素。 http://stackoverflow.com/questions/32693061/angular-2-typescript-get-hold-of-an-element-in-the-theplate/35209681#35209681可能有幫助 –

相關問題