Angular 2 模板组件

标签 angular components

你好,我想创建一个自定义对话框组件,我想以声明的方式插入内容,应该如下所示:

app.action.dialog.component.ts:

@Component({
    selector: 'app-action-dialog',
    templateUrl: 'app/template/app.action.dialog.component.html'  
})
export class ActionDialog {

    showing: boolean;

    constructor() {
        this.showing = false;
    }

    show() {
        this.showing = true;
    }

    hide() {
        this.showing = false;
    }
}

app.action.dialog.component.html:

<div id="overlay" class="valign-wrapper" 
    *ngIf="showing" (click)="hide()">
    <div class="container valign">
        <div class="card">
            <div class="card-content">
                <content select="[content]"></content> 
            </div>
        </div>
    </div>
</div>

使用示例:

<app.action.dialog>
    <div content> example </div>
</app.action.dialog>

这行不通,我该怎么做?可能吗?

最佳答案

我正确理解你的问题(向另一个组件提供一些内容),我认为你可以利用 ng-content:

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

然后像这样使用组件:

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

希望对你有帮助, 蒂埃里

关于Angular 2 模板组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34950950/

相关文章:

c# - 使用 Blazor 将输入文本动态绑定(bind)到类/对象属性

ruby-on-rails - Rails 友好的 DHTML 网格?

javascript - 如何在状态更改时重新渲染子组件(父)reactjs

java - Selenium 不等待元素可点击

angular - 是否可以在 Angular Material 中使用组件作为 mat-grid-tile ?

html - Bootstrap,选择选项占位符仅在首次提交后显示

jsf - 包装 h :form tag in the jsf template page 是最佳实践吗

angular - 在 Angular 2 中存储环境变量

angular - 手动清理字符串

cakephp - 在 CakePHP 中共享应用程序逻辑的正确位置是什么?