简体   繁体   English

Angular2:具有自定义值的模板变量

[英]Angular2: Template variables with custom values

I understand that I can assign local template variables to hold element reference like following 我知道我可以分配本地模板变量来保存元素引用,如下所示

<input #myInput />

But can I declare a variable with custom value? 但是我可以声明具有自定义值的变量吗?

For example: 例如:

<!-- Somewhere I do #obj = model.data.something -->

<input [(ngModel)]="obj.inputA" >
<input [(ngModel)]="obj.inputB" >

Instead of doing 而不是做

<input [(ngModel)]="model.data.something.inputA" >
<input [(ngModel)]="model.data.something.inputB" >

You can't declare a variable with a custom value. 您不能使用自定义值声明变量。

A template variable 模板变量

  • can refer to the element or component it was applied to 可以引用它应用于的元素或组件
  • can refer to a specific directive (if one or more are applied to an element) by referring to its exportAs: 'xxx' property #myVar="xxx" 可以通过引用其exportAs: 'xxx'来引用特定指令(如果将一个或多个应用于元素) exportAs: 'xxx'属性exportAs: 'xxx' #myVar="xxx"
  • can be declared by structural directives like *ngFor="let x of y; let i=index" 可以通过*ngFor="let x of y; let i=index"结构性指令声明*ngFor="let x of y; let i=index"

There is an open issue to support assigning custom expressions as well https://github.com/angular/angular/issues/2451 还有一个开放的问题来支持分配自定义表达式https://github.com/angular/angular/issues/2451

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM