简体   繁体   中英

Is it possible to declare and manipulate a variable (non-element) in a Angular 2 template?

For example, I have a set of inputs and I want to set the 'tab'attribute on them, incrementing down the page. I don't want to say the exact tab number, because I may edit the template later and insert a new input and I don't want to redo every number.

<input [attr.tab]="1" />
<input [attr.tab]="2" />
<input [attr.tab]="3" />

So I'd rather use a variable local to the template , as using a component function generates errors ('Expression '...' has changed after it was checked.')

There are such localized variables in directives like *ngFor, so is it possible to do the same in a component template? Or a way to achieve this effect generally (ie. not just to forms or something)?

Like:

<input [attr.tab]="let i = 1, i" />
<input [attr.tab]="i++" />
<input [attr.tab]="i++" />

Template Syntax Doc: https://angular.io/docs/ts/latest/guide/template-syntax.html

Edit: Not using *ngFor or any template loop, I'm not trying to specify a inner template.

<input [attr.tab]="'1'" #tab1 />
<input [attr.tab]="tab1.getAttribute('tab') + 1" />
<input [attr.tab]="tab1.getAttribute('tab') + 2" />

See also this open issue. https://github.com/angular/angular/issues/2451

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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