我想在Angular 4中的子组件中调用父方法(deletePhone) . 我该如何正确地做到这一点?
我的父组件看起来像:
export class ContactInfo implements OnInit {
phoneForm: FormGroup;
phones: Phone[];
constructor(private fb: FormBuilder,
private userService: UserService) {
}
ngOnInit() {
this.userService.getDataPhones().subscribe(
phones => {
this.phones = phones;
});
this.phoneForm = this.fb.group({
phone: ['', [Validators.pattern(PHONE_PATTERN)]]
});
}
deletePhone(phone: Phone) {
this.userService.deleteUserPhone(phone)
.subscribe(res => {
let index = this.phones.indexOf(phone);
if (index > -1) {
this.phones.splice(index, 1);
}
});
}
}
3 回答
在
ContactInfo
的模板中这对我有用(例如来自官方文档):
https://angular.io/api/core/EventEmitter#examples
儿童:
家长:
我不喜欢像@Output()这样的样板代码 . 我找到了另一种解决方案,只需使用任意数量的匿名函数传递对象
和孩子:
我觉得这样做很安全,不是吗?