亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在Angular 2中獲取與ElementRef關聯的組件的參考

如何在Angular 2中獲取與ElementRef關聯的組件的參考

守著星空守著你 2019-11-15 10:59:22
我的模板中包含以下組件:<vector-editor  #scaleControl                [x]="scaleX"                [y]="scaleY"                [z]="scaleZ" >               </vector-editor>該vector-editor結構如下:export class VerticeControlComponent implements OnInit{    @Input() x: number;    @Input() y: number;    @Input() z: number;    ...}在我的應用程序中,我獲取了有關#scaleControl使用的參考@ViewChild('scaleControl') scaleControl: ElementRef;現在,如果我輸出scaleControl到控制臺,則會得到以下結果:可以看出,引用不是null,并且組件的所有屬性都在其中。我想訪問這些屬性,但是在代碼中,scaleControlis 的實際類型是ElementRef而不是VectorEditorControl在輸出中看到的。因此,我不允許TypeScript使用this.scaleControl.x。我也試過ElementRef像這樣var temp = <VectorEditorControl>this.scaleControl但我得到一個錯誤,告訴我,我不能投ElementRef來VectorEditorControl最后,我嘗試訪問,this.scaleControl.nativeElement但未定義...我在這里想念什么?
查看完整描述

3 回答

?
幕布斯7119047

TA貢獻1794條經驗 獲得超8個贊

在撰寫本文時,我建議使用以下方法:


@ViewChild('scaleControl') scaleControl: ElementRef<VerticeControlComponent>;

然后,您可以訪問這兩種類型。


ElementRef:使用scaleControl

VerticeControlComponent:使用scaleControl.nativeElement


查看完整回答
反對 回復 2019-11-15
  • 3 回答
  • 0 關注
  • 1178 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號