如何在HTML模板中使用let?
我正在尝试做一些简单的事情: 我想在*ngFor中执行一个函数。函数返回一个对象。我想在一种“let”语句中设置该对象,以便在HTML中使用其属性:如何在HTML模板中使用let?,html,angular,let,Html,Angular,Let,我正在尝试做一些简单的事情: 我想在*ngFor中执行一个函数。函数返回一个对象。我想在一种“let”语句中设置该对象,以便在HTML中使用其属性: <div *ngFor="let productGroup of getproductGroupBySomeVariable(variable)"> <!-- This is where I need to set a variable with the output of th
<div *ngFor="let productGroup of getproductGroupBySomeVariable(variable)">
<!-- This is where I need to set a variable with the output of
the getProductBySomeProperty function-->
<div *ngLet="'{{getProductBySomeProperty(productGroup.someproperty)}}'
as myVar" class="ui-g">
<!-- Here I want to use and display the properties of the
object created by the function above-->
<span>{{myVar.property1}} </span>
<span>{{myVar.property2}} </span> etc....
</div>
</div>
{{myVar.property1}}
{{myVar.property2}}}等。。。。
您可以为此创建一个指令,并按如下方式使用:
指令
import{Directive,Input,TemplateRef,ViewContainerRef}来自“@angular/core”;
接口ILetContext{
ngLet:T;
}
@指示({
选择器:“[ngLet]”
})
导出类指令{
私有上下文:ILetContext={ngLet:undefined};
构造函数(viewContainer:ViewContainerRef、templateRef:templateRef){
createEmbeddedView(templateRef,this.context);
}
@输入()
私有集ngLet(值:T){
this.context.ngLet=值;
}
}
Html
<ng-container *ngLet="getproductGroupBySomeVariable(variable) as productGroup;"></ng-container>
我只想提一个非常有效的替代解决方案:
- 单一订阅
- 与虚假价值观打交道
- 是强类型的
{{context.user}}
您不希望在模板中使用这样的函数。它将在每次检测到更改时触发。@AJT_82-你说得对-我收集了它,并将其作为组件网中的数据结构进行排列