javascript - 如何更新其他组件的html文本值
问题描述
这是我的 app.component.html 源代码:
<split-flap></split-flap>
<button (click)="flip('+')">+</button><button (click)="flip('-')">-</button>
这是我的 app.component.ts 源代码:
import { Component } from '@angular/core';
import { SplitFlapComponent } from './split-flap/split-flap.component';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css'],
providers:[SplitFlapComponent]
})
export class AppComponent {
constructor(private splitFlapComponent: SplitFlapComponent)
{
}
flip(flag)
{
console.log(flag);
this.splitFlapComponent.title=flag;
}
}
这是我的 split-flap.component.ts:
import { Component, OnInit, Input, Output } from '@angular/core';
@Component({
selector: 'split-flap',
templateUrl: './split-flap.component.html',
styleUrls: ['./split-flap.component.css']
})
export class SplitFlapComponent implements OnInit {
title: string;
constructor() {
this.title = 'split-flap works1!';
}
ngOnInit() {
}
}
这是我的 split-flap.component.html:
<p>{{title}}</p>
我的 app.component.html 有两个按钮,它们都传递一个符号到split-flap.component
,你能告诉我如何更新 split-flap.component.html 内容吗?因为大多数示例仅适用于输入框,所以我找不到<p>
.
解决方案
试试这样:
解决方案 1:@Input()
app.component.html
<split-flap [title]="title"></split-flap>
app.component.ts
title: string;
constructor() {
this.title = 'split-flap works1!';
}
flip(flag)
{
console.log(flag);
this.title=flag;
}
split-flap.component.ts:
@Input() title: string
解决方案 2:ViewChild()
app.component.html
<split-flap #child ></split-flap>
app.component.ts
@ViewChild('child') child: SplitFlapComponent ;
flip(flag)
{
console.log(flag);
this.child.title=flag;
}
推荐阅读
- sorting - 在 Golang 中对数据类型 []int32 进行排序
- c# - 无法访问在 docker 容器中启动的 ASP.NET 应用程序
- linux - 如何更改 RHEL 服务器服务文件中的 RabbitMQ 配置路径?
- google-cloud-platform - 如何解决 SSH Google Cloud Platform 上的错误 4003?
- php - 如何在php中对数字进行四舍五入?
- r - 有没有办法在函数中拥有类似的 gtsummary 表
- php - Imagekit 不返回转换后的 url
- python - 如何使用动态生成的表单管理 Django 表单验证
- powershell - 使用其他联系人的属性更新 Powershell 中的 Outlook 联系人属性会用空字符串覆盖
- html - 如何使用 css 和 html 为输入元素正确设置文本溢出省略号?