将<input />元素的值作为参数传递给返回undefined的附加事件函数

时间:2017-08-18 16:36:56

标签: html angular events

编辑:好的,我并没有这么认为,显然在模板中传递this.value将不会做我想做的事情。我的问题是:在Angular中是否有一种方法可以让元素引用它自己的属性,比如<input mdInput [value] = "this.currentFrameProperties.index ? this.currentFrameProperties.index : 0" (change) = "OnFrameChanged(this.value)" > 如何在普通的html中工作?

我的Angular项目中有以下输入变量:

this.value

我在OnFrameChanged函数中放置了一个断点,并确定传递给它的参数是未定义的。我很困惑,为什么private OnFrameChanged(frame : number) : void { // At this position, frame is undefined this.webSocketService.send( "jump to frame " + frame); } 不返回input元素的value属性?

这是我的OnFrameChanged()函数:

{{1}}

什么出错的想法?​​

2 个答案:

答案 0 :(得分:1)

您不应该在模板中绑定到此。而是执行以下操作:

<input 
   mdInput 
   [value]   = "currentFrameProperties.index ? currentFrameProperties.index : 0"
   (change)  = "onFrameChanged($event)"
>

并使用camelCase作为方法名称

答案 1 :(得分:1)

显然this关键字在Angular模板中不起作用,但为了重新创建这种行为,我们可以给输入元素一个template reference variable

这使模板能够引用此元素中的值,如下所示:

<input 
   #frameInputElement
   mdInput 
   [value]   = "this.currentFrameProperties.index ? this.currentFrameProperties.index : 0"
   (change)  = "OnFrameChanged(frameInputElement.value)"
>