- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
简而言之,我有一个用作文本输入的组件。父组件使用 @Input
绑定(bind)将数据传递到此组件。当通过 @Output
绑定(bind)触发更改事件时,我在父组件中执行一些自动验证。这是为了删除错误值并用一些合理的默认值替换它们以获得更好的用户体验。
这是父组件的基本版本
@Component({
selector: 'parent',
template: `
<div>
<child [value]="item.key1"
(valueChange)="onValueChange($event)">
</child>
</div>
`
})
export class Parent {
item = {
key1: 'test value',
key2: 'another test value'
};
onValueChange(newValue) {
// Perform some validation and set default
if (newValue.length > 4) {
newValue = newValue.substring(0, 4);
}
this.item.key1 = newValue;
}
}
和子组件
@Component({
selector: 'child',
template: `
<div>
<input type="text"
[(ngModel)]="value"
(blur)="onBlur($event)" />
</div>
`
})
export class Child {
@Input() value: string;
@Output() valueChange = new EventEmitter();
onBlur() {
this.valueChange.emit(this.value);
}
}
我遇到的问题如下:
当在子输入中输入一个值并触发 blur
事件时,新值冒泡到父级并应用验证 - 如果验证导致值被修改,它正确地回到 child 的值(value) - 快乐的日子。
但是,如果前 4 个字符保持不变,并且您追加了其他字符,则在 blur
上仍会应用验证,并且父项的值将正确更新,但子项将保留“无效”值 - 不再有快乐的日子。
所以在我看来 Angular 没有检测到 parent 数据的变化(这很公平,因为它在技术上没有改变)所以它没有将“最新”值发送回 child 。
我的问题是,如何让子文本输入始终显示父项的正确值,即使它在技术上没有“改变”?
最佳答案
更好的解决方案
@Jota.Toledo 的好评让我意识到我的方法,虽然它当时确实是我的一种快速解决方法,但它不是一个好方法,所以我实际上去对我的项目做了一些可行的更改对你也一样,也听从他的建议
Delegating that "validation" logic into the child component
同时将验证定义作为一个函数保留在父级中,该函数作为 @Input
参数传递给子级。
这样我会给父级 2 个公共(public)变量
并更改 onValueChange
函数以仅更新 item.key1,因为它已经过验证。
在 child 中添加一个新的 @Input
类型的 Function
参数(验证)并使用该函数来验证 onBlur 中的新值,然后再将值发送给 parent 。
我觉得我在这里写的内容可能“听起来”有点令人困惑,所以我添加了我想要解释的代码。
parent
@Component({
selector: 'parent',
template: `
<div>
<p><b>This is the parent component</b></p>
<child [value]="item.key1"
[validation]="validation"
(valueChange)="onValueChange($event)">
</child>
<p><b>Variable Value:</b> {{item | json}} </p>
</div>
`
})
export class Parent {
item = {
key1: 'test value',
key2: 'another test value'
};
validation = (newValue) => {
if (newValue.length > 4) {
newValue = newValue.substring(0, 4);
}
return newValue;
}
onValueChange(newValue) {
this.item.key1 = newValue;
}
}
子级(模板部分未更改,因为它未更改)
export class Child {
@Input() value: string;
@Input() validation: Function;
@Output() valueChange = new EventEmitter();
onBlur() {
this.value = this.validation(this.value)
this.valueChange.emit(this.value);
}
}
以前的答案(不是一个好方法)
我过去遇到过类似的问题,我解决的方法是清除我的 var,然后在 setTimeout
中给它最终值而不指定毫秒数。
在你的父组件中它看起来像这样:
onValueChange(newValue) {
console.log('Initial: ' + newValue);
if (newValue.length > 4) {
newValue = newValue.substring(0, 4);
}
console.log('Final: ' + newValue);
this.item.key1 = '';
setTimeout(() => {
this.item.key1 = newValue;
});
}
关于javascript - Angular 父子绑定(bind)变化检测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57253822/
我正在开发我的第一个核心数据支持的应用程序,但无法弄清楚如何正确设置 NSFetchedResultsController。我有两个实体: /-----------\ /--------
我是 javax.swing.* 包的新手,只熟悉 c#.net。我有两个 JFrame,分别是 frmLogin 和 frmMain。我想要做的只是一个像这样的简单代码: +----- C# 版本
我正在从 CoreData 迁移到 Realm...本质上我需要有两个独立的数据库,假设一个只有内存,第二个有磁盘持久性 现在在解析期间,我需要创建一个可以在给定线程中工作但与我选择的顶级 Realm
下面是我的表中的内容。 我的表格 ++++++++++++++++++++ Parent + Child ++++++++++++++++++++ C1 + G1 C1
好的,让我们设置一个场景。 场景: 您有一个内容 Controller 。该内容可能是一张照片、一篇博文等等。现在,在此内容的 html 中,您有一个 Comment Controller 。 Com
我有两个脚本 parent.sh 和 child.sh。 parent.sh 中有一个变量需要被子进程访问。我已经通过在父脚本中导出变量来实现这一点,并且该变量可用于子进程? 有什么方法可以让 chi
我见过类似但不完全相同的请求。 如果我有下表 Parent Child 1 2 1 3 4 3 5 1 6 1 5 7 8
这类似于问题 ( Finding parents in a tree hierarchy for a given child LINQ (lambda expression) )。但是,我不需要找到所
好的,所以我是 C# 做事的新手,我来自 ruby 世界。 我有一个一对多的关系(为了这个问题, parent 对 child ),出于某种原因,L2S 想要创建一个新的 parent 而不是使用
我想为我的网站创建一个完全由数据驱动的面包屑。 数据使用 MariaDB 存储,如下所示: parent_id | parent_name | child_id | child_name ——————
所以,我正在查看 Parse Anypic 教程中的代码 here 我的问题是: 有这 2 个 View Controller : @interface PAPHomeViewController :
我正在使用 CSS 处理树结构层次结构,我需要一些帮助。我想让两个父子链接起来,如果我删除字符 a,我将无法查看 CSS ::after 选择器中定义的 border-left 来自内容元素。 这是我
我对实现以下目标有点困惑1.禁用一个父div的可见性2. 但另一个代码允许子 div 可见性 http://jsfiddle.net/cbXxU/ 如果父 div 不可见或对此
我正在创建一个水平的 ul 导航栏。每个 li 元素都有不同的宽度。我想在下拉菜单中做到这一点,下拉菜单中的“子级” li 项的长度/宽度与“父级” li 项的长度/宽度相同。正在考虑使用 jQuer
我有这个问题很长时间了,我已经在网上和SO进进出出搜索,但还没有找到解决方案。我希望你能帮助我。 我有两个实体之间的父子关系,如下所示: @Entity public class Parent {
我需要在我的网页上找到一个与其他元素具有相同标签的元素,因此我需要根据子部分[parent]的标题找到xpath。 以下是网页的html代码: 基本上,我想根据上图中的 Scheduled 文本在 l
我有 2 张 table .. Adult ------------------- id ParentChild ------------------- parentID(adult's id) ch
我想创建一个函数来创建彼此具有分层关系的对象。因此,每个层对象都拥有自己的一组子层对象,并与其所有兄弟对象共享一个父对象。我不熟悉任何模式,但我想应该有一个模式可以涵盖这种情况。 //construc
我有一个父 div 和一个子 div。这里是 html Parent Child Div Js var parent = document.createElement("parent");
我正在开发一个 reactjs 应用程序 - 我正在分解一个大组件以拥有一个子组件。我在 child 中创建了一个回调函数,它将返回给 parent 。当一个复选框被选中时——子组件执行回调并返回到父
我是一名优秀的程序员,十分优秀!