Angular - @Input 的双向数据绑定不起作用
Angular - two way data binding from @Input not working
我正在尝试通过将其余部分重置为 0 来实现仅接受数值的自定义输入,对输入组件使用以下代码:
import {Component, Input, Output, ElementRef, EventEmitter} from '@angular/core';
import {Observable} from 'rxjs/Rx';
@Component({
selector: 'debounce-input',
template: '<input type="text" [placeholder]="placeholder" [(ngModel)]="_v">'
})
export class DebounceInputComponent {
@Input() placeholder: string
@Input() delay: number = 300
_v: string
@Input()
get v(): string {
return this._v
}
set v(_value) {
this._v = _value
this.valueChange.emit(this.v)
}
@Output() valueChange: EventEmitter<any> = new EventEmitter<any>()
@Output() value: EventEmitter<any> = new EventEmitter<any>()
constructor(private elementRef: ElementRef) {
const eventStream = Observable.fromEvent(elementRef.nativeElement, 'keyup')
.map(() => this.v)
.debounceTime(this.delay)
.distinctUntilChanged()
eventStream.subscribe((obj) => this.value.emit({v: this.v}))
}
}
上面的组件是使用以下代码在 AppComponent 中导入的:
HTML部分:
<div style="text-align:center">
<debounce-input [v]="mynumber"
delay="1000"
placeholder="Type something..."
(value)="handle($event)">
</debounce-input>
</div>
TypeScript 部分:
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
title = 'app';
mynumber = 0
handle(obj) {
console.log(obj.v)
if (isNaN(Number(obj.v))) {
console.log('trying to reset invalid input to 0')
this.mynumber = 0
}
}}
问题是每当我键入一个非数字值时,AppComponent
无法将输入重置为 0,尽管 "trying to reset invalid input to 0" 消息显示在控制台中。
这种行为的原因是什么?
修改输入组件中的 setter,使 ngModel 指向 v 而不是 _v。
import {Component, Input, Output, ElementRef, EventEmitter} from '@angular/core';
import {Observable} from 'rxjs/Rx';
@Component({
selector: 'debounce-input',
template: '<input type="text" [placeholder]="placeholder" [(ngModel)]="v">'
})
export class DebounceInputComponent {
@Input() placeholder: string
@Input() delay: number = 300
_v: string
@Input()
get v(): string {
return this._v
}
set v(_value) {
if (isNaN(Number(_value))) {
console.log('trying to reset invalid input to 0')
this._v = '0';
return;
}
this._v = _value
this.valueChange.emit(this.v)
}
@Output() valueChange: EventEmitter<any> = new EventEmitter<any>()
@Output() value: EventEmitter<any> = new EventEmitter<any>()
constructor(private elementRef: ElementRef) {
const eventStream = Observable.fromEvent(elementRef.nativeElement, 'keyup')
.map(() => this.v)
.debounceTime(this.delay)
.distinctUntilChanged()
eventStream.subscribe((obj) => this.value.emit({v: this.v}))
}
}
我明白了。您的 @Input 值未更改,因为父项中的 mynumber
未从子项更改中更改。我添加了这个功能并且它起作用了:
handle(obj) {
console.log(obj.v)
if (isNaN(Number(obj.v))) {
console.log('trying to reset invalid input to 0')
this.mynumber = 0;
} else {
this.mynumber = obj.v; // add this line
}
}
P.S。
但是,我仍然会考虑将您的输入限制为仅数字类型并添加一些过滤器以防止设置像“0123”这样的数字。
我正在尝试通过将其余部分重置为 0 来实现仅接受数值的自定义输入,对输入组件使用以下代码:
import {Component, Input, Output, ElementRef, EventEmitter} from '@angular/core';
import {Observable} from 'rxjs/Rx';
@Component({
selector: 'debounce-input',
template: '<input type="text" [placeholder]="placeholder" [(ngModel)]="_v">'
})
export class DebounceInputComponent {
@Input() placeholder: string
@Input() delay: number = 300
_v: string
@Input()
get v(): string {
return this._v
}
set v(_value) {
this._v = _value
this.valueChange.emit(this.v)
}
@Output() valueChange: EventEmitter<any> = new EventEmitter<any>()
@Output() value: EventEmitter<any> = new EventEmitter<any>()
constructor(private elementRef: ElementRef) {
const eventStream = Observable.fromEvent(elementRef.nativeElement, 'keyup')
.map(() => this.v)
.debounceTime(this.delay)
.distinctUntilChanged()
eventStream.subscribe((obj) => this.value.emit({v: this.v}))
}
}
上面的组件是使用以下代码在 AppComponent 中导入的:
HTML部分:
<div style="text-align:center"> <debounce-input [v]="mynumber" delay="1000" placeholder="Type something..." (value)="handle($event)"> </debounce-input> </div>
TypeScript 部分:
import { Component } from '@angular/core'; @Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.css'] }) export class AppComponent { title = 'app'; mynumber = 0 handle(obj) { console.log(obj.v) if (isNaN(Number(obj.v))) { console.log('trying to reset invalid input to 0') this.mynumber = 0 } }}
问题是每当我键入一个非数字值时,AppComponent
无法将输入重置为 0,尽管 "trying to reset invalid input to 0" 消息显示在控制台中。
这种行为的原因是什么?
修改输入组件中的 setter,使 ngModel 指向 v 而不是 _v。
import {Component, Input, Output, ElementRef, EventEmitter} from '@angular/core';
import {Observable} from 'rxjs/Rx';
@Component({
selector: 'debounce-input',
template: '<input type="text" [placeholder]="placeholder" [(ngModel)]="v">'
})
export class DebounceInputComponent {
@Input() placeholder: string
@Input() delay: number = 300
_v: string
@Input()
get v(): string {
return this._v
}
set v(_value) {
if (isNaN(Number(_value))) {
console.log('trying to reset invalid input to 0')
this._v = '0';
return;
}
this._v = _value
this.valueChange.emit(this.v)
}
@Output() valueChange: EventEmitter<any> = new EventEmitter<any>()
@Output() value: EventEmitter<any> = new EventEmitter<any>()
constructor(private elementRef: ElementRef) {
const eventStream = Observable.fromEvent(elementRef.nativeElement, 'keyup')
.map(() => this.v)
.debounceTime(this.delay)
.distinctUntilChanged()
eventStream.subscribe((obj) => this.value.emit({v: this.v}))
}
}
我明白了。您的 @Input 值未更改,因为父项中的 mynumber
未从子项更改中更改。我添加了这个功能并且它起作用了:
handle(obj) {
console.log(obj.v)
if (isNaN(Number(obj.v))) {
console.log('trying to reset invalid input to 0')
this.mynumber = 0;
} else {
this.mynumber = obj.v; // add this line
}
}
P.S。 但是,我仍然会考虑将您的输入限制为仅数字类型并添加一些过滤器以防止设置像“0123”这样的数字。