[英]Instantiate class in component in Angular
目前有這個
import { Component, Input, OnInit, ViewEncapsulation } from '@angular/core';
import { RsForm } from './rs-form-model/rs-form.model';
@Component({
selector: 'rs-form',
templateUrl: './rs-form.component.html',
styleUrls: ['./rs-form.component.scss'],
encapsulation: ViewEncapsulation.None
})
export class RsFormComponent {
@Input() form;
constructor(){}
ngOnInit(){
this.form = new RsForm(this.form);
}
@Input形式作為json到達。
我想建立一個類的新實例並移交給this.form。
有沒有辦法將JSON傳遞給組件構造函數,以便DI 使用 JSON數據創建實例? 如...
constructor(private form: RsForm){
this.form // would already have the JSON data attached as well as
// RsForm class functions.
你不能這樣做
// import 'RsForm' type here
export class RsFormClass {
form; // if you know what 'type' it is, specify it
// and other properties...
constructor(form: RsForm) { // don't forget to import the RsForm interface type in the import section at the top
this.form = form;
}
}
在您的組件中
// after all the necessary imports including the one for 'RsFormClass'
export class RsFormComponent implements OnInit {
@Input() form;
constructor(){}
ngOnInit(){
this.form = new RsFormClass(this.form);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.