[英]Using ng-template and ngIf together where ngIf can access the paased argument
<div>
<h3>All Templates</h3>
<ul *ngFor="let number of numbers">
<ng-container [ngTemplateOutlet]='number % 2 == 0 ? even_tpl : odd_tpl' [ngTemplateOutletContext]="{number:number}"></ng-container>
</ul>
</div>
<ng-template #odd_tpl let-number='number'>
<li>Odd: {{number}}</li>
</ng-template>
<ng-template #even_tpl let-number='number'>
<li>Even: {{number}}</li>
</ng-template>
PS. PS。 sample code from Vivek Doshi's answer here .
来自 Vivek Doshi 的答案的示例代码。
but what I'm looking for is to only go-inside ng-template when a condition meets, something like this:但是我正在寻找的是仅在满足条件时才进入 ng-template ,如下所示:
<ng-template #odd_tpl let-number='number' *ngIf="number > 10">
<li>Odd: {{number}}</li>
</ng-template>
but it is showing an error:但它显示错误:
Identifier 'number' is not defined.
未定义标识符“编号”。 The component declaration, template variable declarations, and element references do not contain such a member
ng
.组件声明、模板变量声明和元素引用不包含这样的成员
ng
。
I want to access the passed argument number
, is this possible?我想访问传递的参数
number
,这可能吗?
So as per yurzui commented I've used ng-container, previously there was some element getting added to the DOM but it was a mistake which I fixed and now things are works just fine.因此,根据 yurzui 的评论,我使用了 ng-container,之前有一些元素被添加到 DOM 中,但这是我修复的错误,现在一切正常。
I can check for the condition like this -我可以检查这样的情况 -
<ng-template #odd_tpl let-number='number'>
<ng-container *ngIf="number > 10">
<li>Odd: {{number}}</li>
</ng-container>
</ng-template>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.