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

时间:2018-09-11 03:40:56

标签: angular typescript ng-template

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

这给了我找不到“年份”的错误

NestedScrollView

2 个答案:

答案 0 :(得分:3)

您需要传递“ 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'
    }
  }

答案 1 :(得分:0)

一种可以使用的技术是滥用? *ngIf(检查支持它的角度版本)

<ng-container *ngIf="{color:getColor(year,i,dataItem.ca)}; let state">
     {{state.color.Date}}
</ng-container>
  • *ngIf 允许将表达式的结果存储在变量中。
  • 因为创建了一个对象,*ngIf 将始终是真实的,因此渲染 ng-container
  • ng-container 不呈现为 dom 元素,仅呈现其内容