angular2 let-entity指令 [英] angular2 let-entity directive

查看:172
本文介绍了angular2 let-entity指令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个包含模板的组件:

I've create a component which takes in a template:

export class TemplateParamComponent implements OnInit {
    @Input() items: Array<any>;
    @Input() template: TemplateRef<any>;
 }

使用html:

<template #defaultTemplate let-item="item">
    <strong>{{item.name}}</strong>
</template>
<ul>
    <li *ngFor="let item of items">
        <template [ngTemplateOutlet]="template || defaultTemplate" [ngOutletContext]="{item: item}"></template>
    </li>
</ul>

我对let-item指令的功能感到困惑,这会创建一个名为item的变量吗?是一个特殊的指令,可让您声明要在html范围内使用的变量吗?

I'm confused on what the directive let-item does, does that create a variable called item? is let a special directive that lets you declare variable to use in scope for html?

推荐答案

了解角柄<template>元素如何阅读文档的最佳建议:

Best suggestion for you to understand how angular handle <template> element is reading documentation: https://angular.io/guide/structural-directives

长话短说:将<template>作为函数(方法),将let-something作为输入变量.所以

Long story short: consider <template> as function (method) and let-something as input variable. So

<template #defaultTemplate let-item="item">
     <strong>{{item.name}}</strong>
</template>

可以读为

function defaultTemplate(item: any){
    return "<strong>" + item.name + "</strong>";
}

这篇关于angular2 let-entity指令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆