我想在自定义指令中绑定“ngFor”索引作为属性 我做错了什么?Angular 2指令绑定
验证码:
@Directive({selector: '[closeWeb]'})
class CountClicks {
numberOfClicks = 0;
@HostListener('touchstart', ['$event.target'])
touchstart(btn) {
console.log(btn)
}
}
@Component({
selector: 'wrap',
template: `
<div class="wrap">
<div class="item" *ngFor="#webviews of webviewsCount; #i = index">
<!-- I want to bind index in custom directive as attribute -->
<div [closeWeb]="i">{{i}}</div>
</div>
</div>
`,
directives: [CountClicks]
})
export class Wrap {}
而且我得到这个错误:
EXCEPTION: Template parse errors:
Can't bind to 'closeWeb' since it isn't a known native property ("</div><div [ERROR ->][closeWeb]="i">{{i}}</div></div></div>"): [email protected]:21
不幸的是,它并不能帮到我。然后我添加注释,错误消失,但然后我点击元素,我只有标签没有指令'
实际上,该指令实际上是应用的。我的意思是你可以点击几次元素。看到这个plunkr:https://plnkr.co/edit/Lm4qnfcAhmTDXpELh6ib?p=preview。为什么你需要'closeweb'zttribute仍然存在? –
工作,非常感谢!这个索引作为属性需要我在业务逻辑中的其他功能(我将代码从Angular 1.4升级到Angular 2)。但是我有一个问题,那么你在这段代码中替换'touchstart'到'click'' @HostListener('touchstart',['$ event.target']); touchstart(btn){console.log('click ='+ btn +' - '+ this.numberOfClicks); }' - 你实现click作为touchstart事件,对吧? –