【发布时间】:2019-06-26 00:45:09
【问题描述】:
我想使用 Angular(打字稿)与 2 个组件(父级和子级)共享一个字符串变量。 我使用这种方法但我不喜欢它,因为当我更新输入变量时,子组件会自动更新;我只想在父组件将数据发送到子组件时更新子组件。 我该怎么做?
这是我的父组件.html
<div>
<mat-form-field>
<input matInput type="text" [(ngModel)]="city">
</mat-form-field>
<button mat-button (click)="getWeather()">
<mat-icon matSuffix>search</mat-icon>
</button>
</div>
<app-city-weather [testCity]="city"></app-city-weather>
这是我的父组件.ts
city: string;
getWeather() {
// I make an http request, but doesn't matter
}
这是我的子组件.html
<p>
Child compoent it works!
</p>
这是子组件.ts
@Input() testCity: string;
【问题讨论】:
-
你在哪里调用父模板中的孩子
-
对不起,我忘记写了。我更新了问题!
-
如何将天气传递给子组件?您能否添加
getWeather()与子组件的交互方式。
标签: angular typescript parent-child angular7