css - 在 Angular 7 组件之间共享数据
问题描述
我想将age
输入中的数据从child-information
组件发送到age-line
包含垂直线的组件。'age * 100 pixels'
当它到达年龄时,这条线向左移动。
我的代码有什么问题?没有错误消息。
儿童inf.html:
<button id="okButton" (click)=addAge()>OK</button>
Age : <input
type="text" name="Age" value="Kor" id="ageId" onfocus="this.value =
this.value=='Kor'?'':this.value;" onblur="this.value =
this.value==''?'Kor':this.value;">
Child-inf.ts:
export class ChildInformationsComponent implements OnInit {
age:number = 1;
@Output() buttonClicked: EventEmitter<number> = new EventEmitter<number>();
constructor(private el: ElementRef) { }
ngOnInit() { }
addAge(){
this.age =parseInt((<HTMLInputElement>document.getElementById('ageId')).value);
this.buttonClicked.emit(this.age);
}
}
年龄线html:
<div class="age-line" [style.left.px]="age * 100"> <div class="line"></div> </div>
年龄线 ts:
export class AgeLineComponent implements OnInit {
constructor() { }
age: number= 1;
showNextComponent(emittedAge: number) {
this.age = emittedAge;
console.log('Button clicked');
}
ngOnInit() { }
}
解决方案
在 Angular 中,当您需要在 Child-Parent 关系中将值从子组件发送到其父组件时,您可以使用Output() 。
我没有看到Age line
Component 小时候有Child-inf
.
如果你想要那个Child-inf
并且Age line
有一个子父关系,你应该Child-inf
在Age line
Html.xml 中包含它。
年龄线html:
<div class="age-line" [style.left.px]="age * 100"> <div class="line"></div> </div>
<child-inf></child-inf>
这里我假设Child-inf
组件选择器是:child-inf
然后如果你想在Child-inf
Component 中监听 Output Event,在 Age line
html 中你必须这样做:
年龄线html:
<div class="age-line" [style.left.px]="age * 100"> <div class="line"></div> </div>
<child-inf (buttonClicked)="showNextComponent($event)"></child-inf>
推荐阅读
- java - 从用户获取字符串输入的替代方法
- angular - 如何扩展或修改 ESLint 规则而不是覆盖
- javascript - 使用 fetch api 发送函数参数
- delphi - 使用此表单的句柄在表单/框架的 OnCreate 中启动线程
- python - 使用 pandas 时的性能问题适用于大数据帧
- visual-studio - 将 vs 2017 配置与 vs 2019 一起使用
- web - 在 WebLogic 12C 中部署 ear 模块时获取 Java.lang.ClassNotFoundException:org.apach.logging.log4j.Logger
- ios - 通过导航控制器传递索引路径 - 更新 CoreData 记录
- sql - 在 PostgreSQL 中使用 IF 语句更新只能有 2 个值的列
- r - 导入字体并在 ggplot 中使用它