angular - 如何在<ng-template>中调用函数让变量声明Angular 4

标签 angular typescript ng-template

我有一个函数,它从 typescript 函数返回颜色、文本颜色和日期值作为对象。我想将其存储在一个 let 变量中。我可以直接使用函数但不想复制函数的调用。

这给我错误,如未找到“年份”

<kendo-grid-column-group title="{{year}}" [headerStyle]="{'text-align': 'center'}" width="380">
        <ul *ngFor="let month of keys(); let i = index">
          <li>
            <kendo-grid-column field="{{month}}" class="no-padding" title="{{month}}" [filterable]="false" [sortable]="false" width="35">
              <ng-template kendoGridCellTemplate let-dataItem let-color="getColor(year,i,dataItem.ca)">
                <span class="whole-cell" [ngStyle]="{'background-color': color.color,'color': color.textColor,'font-weight':'bold','height':'25px','vertical-align': 'middle'}">
                  <label>{{color.Date}}</label>
                </span>
              </ng-template>
            </kendo-grid-column>
          </li>
        </ul>
</kendo-grid-column-group>

最佳答案

您需要传递 'content:this' 范围对象。

模板:

<ng-template let-color="getColor()" #loading>
                <span class="whole-cell" [ngStyle]="{'background-color': color.color,'color': color.textColor,'font-weight':'bold','height':'25px','vertical-align': 'middle'}">
                  <label>{{color.Date}}</label>
                </span>
              </ng-template>

<ng-container *ngTemplateOutlet="loading;context:this"></ng-container>

组件:

getColor() {
    return {
      color: 'red',
      textColor: 'blue',
      Date: 'hi'
    }
  }

关于angular - 如何在<ng-template>中调用函数让变量声明Angular 4,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52268326/

相关文章:

android - Ionic2: Http 发送 {"isTrusted":true} - 与其他可观察对象冲突?

angular - 从数组中观察

typescript - 在 TypeScript 中,如何将 Union 内的所有类型转换为该类型的数组

html - 如何在 Angular 2 中包含没有条件的 ng-template 元素

angular - 使用 *ngTemplateOutlet 的条件渲染在 formGroup 内部无法正常工作

javascript - 如何在 ionic 中自定义 ionic 选择弹出高度

angular - 有没有办法以 Angular 阻止路径?

javascript - 对象中键值的 TypeScript 类型

node.js - 在 sequelize.js 中获取与 belongsToMany 的关联

angular - 在带有模板的 Angular ng-bootstrap 模式中,关闭和关闭按钮不起作用