Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 角度2:获取event.target.value_Angular_Typescript - Fatal编程技术网

Angular 角度2:获取event.target.value

Angular 角度2:获取event.target.value,angular,typescript,Angular,Typescript,我想通过下面的代码获取event.target.value <input mdInput #owner required placeholder="荷主" [formControl]="detail.ownerTx" [mdAutocomplete]="autoTxt" (change)="detail.changeOwner(owner.value)" > cl

我想通过下面的代码获取
event.target.value

    <input 
      mdInput 
      #owner
      required 
      placeholder="荷主" 
      [formControl]="detail.ownerTx" 
      [mdAutocomplete]="autoTxt" 
      (change)="detail.changeOwner(owner.value)"
     >

   class Detail {
    changeOwner(val: string){
     console.log(val);
    }
   }

课堂细节{
变更所有者(val:string){
控制台日志(val);
}
}
但是
console.log(val)
的答案是什么。。。。有什么想法可以实际进行数据绑定吗

(input)="detail.changeOwner($event.target.value)"

您还可以在表单控件(
detail.ownerTx
)上订阅
valueChanges

另请参见将输入行更改为此

<input 
      mdInput 
      #owner
      required 
      placeholder="荷主" 
      [formControl]="detail.ownerTx" 
      [mdAutocomplete]="autoTxt" 
      (change)="detail.changeOwner($event)">

我认为您应该使用(keyup)或其他键盘事件处理程序,如果您想使用(keyup)=“changeInput($event)”获取此值,那么您可以访问DOM事件和值;)

使用组件方法实现强制转换,无法在模板中强制转换

@Component({
  selector: '...',
  template: '<input (keyup)="filter(target($event).value)">',
})
export class SomeCmp {
  target(event: KeyboardEvent): HTMLInputElement {
    if (!(event.target instanceof HTMLInputElement)) {
      throw new Error("wrong target");
    }
    return event.target;
  }
}
@组件({
选择器:“…”,
模板:“”,
})
导出类{
目标(事件:KeyboardEvent):HTMLInputElement{
if(!(event.target instanceof HTMLInputElement)){
抛出新错误(“错误目标”);
}
返回事件.target;
}
}

另请参见

什么不起作用?“不起作用”的确切含义是什么?对,在类详细信息中的
console.log(“val:+val)
,并记录
val:
。里面什么都没有。我更新了我的答案。请参阅,非常感谢您提供的建议甚至示例,但不知何故(输入)并没有让console显示其价值。我认为这与
mdAutocomplete
有关。对不起,我不明白你说的“不让console显示值”是什么意思。它在我的stackblitz中打印到控制台上
class Detail {
    changeOwner($event){
     //You will get the target with bunch of other options as well.  
     console.log($event.target.value);
    }
   }
@Component({
  selector: '...',
  template: '<input (keyup)="filter(target($event).value)">',
})
export class SomeCmp {
  target(event: KeyboardEvent): HTMLInputElement {
    if (!(event.target instanceof HTMLInputElement)) {
      throw new Error("wrong target");
    }
    return event.target;
  }
}