angular - 如何将父标题更改为Angular中子标题的值?
问题描述
我正在寻找可以在父组件中动态切换(取决于我们所在的组件)值的解决方案。每个组件的值应该不同。
这种解决方案的服务是不是太多了?Angular 中是否有更简单的解决方案?
父级 - 一些文本 {{ title }}
Child1 - 标题 = “标题一” Child2 - 标题 = “标题二”
等等
解决方案
尝试使用EventEmitter:
子组件.ts
import { Output, EventEmitter } from '@angular/core';
title: string;
@Output() titleChange: EventEmitter<any> = new EventEmitter();
ngOnInit() {
this.title = 'child title';
this.titleChange.emit(this.title);
}
父组件.html
<p>{{title}}</p>
<app-child (titleChange)="onTitleChange($event)"></app-child>
父组件.ts
title: string
onTitleChange(value: string) {
this.title = value;
}
推荐阅读
- ruby-on-rails - 无法在 ReactJS 中使用 axios.delete
- dynamics-crm - 基于两个相关实体的属性的 FetchXML 或条件
- python-3.x - 如何从从 keras 导入的 MNIST 数据集中删除特定数字?
- django - 我想在 Django 中为用户添加权限?
- macos - 为什么我不能在 mac os 上 pip install nltk?
- mesh - 在Meshlab中运行Screened Poisson Reconstruction后去除表面的异常值部分
- python - 无法使用 Python 3.8.0 在命令行上运行 python 文件;它改为调用 python 解释器
- angular - 将 nebular 与 font-awesome 集成时的问题
- java - 有这个 java.lang.ArrayIndexOutOfBoundsException 错误阻止我继续我的循环迭代
- apache - 反向代理将不同的名称定向到不同的端口