我需要一些帮助,通过文本输入类设置变量,角2变变量通过输入
nameChange(str){
this.service.setName(str);
console.log(str);
}
有一个例子类,它修改变量,那么应该如何投入的样子,当我只需要在输入更改时随时修改变量?
我需要一些帮助,通过文本输入类设置变量,角2变变量通过输入
nameChange(str){
this.service.setName(str);
console.log(str);
}
有一个例子类,它修改变量,那么应该如何投入的样子,当我只需要在输入更改时随时修改变量?
可以拦截与一个二传手输入属性发生变化,试试这个:
//组件:
@Component({
selector: 'my-component',
template: '<h3> My component </h3>'
})
export class MyComponent {
@Input()
set name(str: string) {
this.service.setName(str);
console.log(str);
}
}
// HTML其中组件用于:
<my-component [name]="Bombasto"></my-component>
您可以使用双向数据绑定。
例子:
<input [(ngModel)]="property">
<p>{{property}}</p>
看看here。
如果你想打电话像一个代码中的功能,使用此:
<input (input)="nameChange($event.target.value)">
您必须使用ngOnChange这种情况下的事件如下。因此,只要你输入变量myVar
值改变,它会调用ngOnChanges
事件
@Input() myVar:any;
ngOnChanges(changes: any) {
if (changes.myVar != null && changes.myVar.currentValue != null) {
//your logic to update any variable or other....
}
}
如果你正在寻找更细粒度的控制,来看看这篇文章:HTTPS://stackoverflow.com/questions/32051273/angular2 - 和反跳 –