Angular 2 Template Component

后端 未结 2 1757
没有蜡笔的小新
没有蜡笔的小新 2020-12-19 15:28

Hello I want to create a custom dialog component and I want to insert it content on a declarative way, should look like this:

app.action.dialog.component.ts:

相关标签:
2条回答
  • 2020-12-19 16:00

    I correctly understand your question (provide some content to another component), I think that you could leverage ng-content:

    @Component({
      selector: 'field',
      template: `
        <div>
          <ng-content></ng-content>
        </div>
      `
    })
    export class FormFieldComponent {
      (...)
    }
    

    and use the component like that:

    <field>
      <input [(ngModel)]="company.address.street"/>
    </field>
    

    Hope it hepls you, Thierry

    0 讨论(0)
  • 2020-12-19 16:04

    I think for <content> to work you need to switch from the default ViewEncapsulation.Emulated to ViewEncapsulation.Native (and add web-components polyfills on browsers that don't support it natively) or use <ng-content> instead which works in all view encapsulation modes.

    0 讨论(0)
提交回复
热议问题