如何在组件内显示动态控件

时间:2017-06-16 12:14:03

标签: angular angular2-directives angular2-components

我正在尝试构建一个基于输入属性呈现其html控件的自定义组件。我遇到的问题是确保自定义组件上的所有指令都添加到相应的html控件中。也许我错了?任何见解都会很棒。

<custom-attribute [type]="'dropdown'" [style.display]="'block'" [(ngModel)]="message">

@Component({
  selector: 'custom-attribute',
  template: 
    `
    <input type="text" *ngIf="type === 'text'">
    <select *ngIf="type === 'dropdown'">
    `
})
export class CustomAttribute {
  @Input("type") type: string;
}

1 个答案:

答案 0 :(得分:0)

试试这个:

<custom-attribute [type]="'dropdown'" [style.display]="'block'" [(message)]="message">

@Component({
    selector: 'custom-attribute',
    template: 
    `
        <input [(ngModel)]="message" type="text" *ngIf="type === 'text'">
        <select *ngIf="type === 'dropdown'">
    `
     })

    export class CustomAttribute {
      @Input("type") type: string;
      @Input('message') model:any;
    }