Angular - 在一个组件中的多个位置使用相同的 ng-template

标签 angular ng-template

具有相同数据源的两个剑道组合框。有什么方法可以将第一个组合框中的 ng-template 重用到第二个组合框中吗?谢谢:)

<kendo-combobox #customerComboboxFrom [data]="customersData" [valueField]="'Name'" [textField]="'Name'" [(ngModel)]="journey.FromCustomer">
        <ng-template kendoComboBoxItemTemplate let-dataItem #customerOverview>
          <img [src]="dataItem.PhotoUrl" alt="">
          <div class="CustomerDetail">
            <span>{{dataItem.Name}}</span>
            <span class="lighter">{{dataItem.State}}, {{dataItem.Country}}, {{dataItem.District}}</span>
          </div>
        </ng-template>
      </kendo-combobox>
      <kendo-combobox #customerComboboxTo [data]="customersData" [valueField]="'Name'" [textField]="'Name'" [(ngModel)]="journey.FromCustomer">
      -----------  //Is there any way how to reuse HERE ng-template #customerOverview from above ?
      </kendo-combobox>

最佳答案

您可以使用 template outlet 将模板插入到第二个组合框中。 。作为替代方案,模板可以在组合框外部定义并插入到两个组合框的定义中。

<kendo-combobox #customerComboboxFrom [data]="customersData" ... >
  <ng-template kendoComboBoxItemTemplate let-dataItem #customerOverview>
    <img [src]="dataItem.PhotoUrl" alt="">
    <div class="CustomerDetail">
      <span>{{dataItem.Name}}</span>
      <span class="lighter">
        {{dataItem.State}}, {{dataItem.Country}}, {{dataItem.District}}
      </span>
    </div>
  </ng-template>
</kendo-combobox>

<kendo-combobox #customerComboboxTo [data]="customersData" ... >
  <ng-template kendoComboBoxItemTemplate let-dataItem>
    <ng-container *ngTemplateOutlet="customerOverview; context: { $implicit: dataItem }">
    </ng-container>
  </ng-template>    
</kendo-combobox>

参见this stackblitz进行演示。

关于Angular - 在一个组件中的多个位置使用相同的 ng-template,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53022913/

相关文章:

Angular,使用外部模板作为 ng-template

angular - <ng-template></ng-template> 正在加载多次

java - Spring CORS 和 Angular 不工作 : HTTP status code 403 error

javascript - typescript /Javascript : Setting a unique list with an additional property

angular - createEmbeddedView 和 createComponent 有什么区别?

Angular 4 : How to update value of array in another component from another component

angular - 将 ng-template 和 ngIf 一起使用,ngIf 可以访问 paased 参数

angular - 路由器 getCurrentNavigation 始终返回 null

javascript - 如何将父组件中声明的 ng-template 中的变量传递给子组件/指令?

angular - 在同一个组件中重复 html