【发布时间】:2018-01-23 11:26:57
【问题描述】:
所以基本上,我制作了一个简单地显示对象数据的通用组件。通过单击按钮发送数据。因此,通用组件在 OnInit 生命周期挂钩期间读取数据,如下所示。
ngOnInit() {
this.settingsService.getAllSettings().responseData()
.subscribe(settingsStream => { this.settingsList = settingsStream.result }, (err) => {
// Error Messages
},
() => {
// Complete Messages
this.currentSetting = this.settingsList
.findIndex(setting => setting.configurationName === this.settingsService.selectedSetting.selectConfig);
})
}
一旦找到配置名称并将其与所选名称匹配。它像这样在模板中显示正确的值。
<input size="150" [value]="settingsList[currentSetting].configurationName" [(ngModel)]="settingsList[currentSetting].configurationName">
但是,可以通过单击按钮更改通用组件数据。但是,由于通用组件仅在 OnInit 期间读取数据,因此它不会更改其显示的数据。我尝试使用 onChanges 和 doCheck 生命周期钩子,但是它们不允许我更改数据,因为它一直在寻找更改。
这是我在模板中的方法,它通过单击按钮激活和显示组件
<div *ngFor="let settings of settingsList">
<button class="btn btn-secondary"
(click)="changeSection(settings.configurationName)"
(click)="domElement='Generic'"> {{settings.configurationName}} </button>
</div>
<div *ngIf="showNewConfig">
<new-config> </new-config>
我以前使用单独的组件进行此操作,但这似乎效率低下并且代码重复。
所有帮助将不胜感激,非常感谢!
【问题讨论】:
-
你有 (click) 两次 (click)="changeSection(settings.configurationName)" (click)="domElement='Generic'"
-
@Vega 据我所知,这不应该影响通用组件更新数据更改的工作
-
我没有说这就是问题所在。我指出了一些不连贯的代码
-
@Vega 有没有办法将单击事件绑定到两个东西?
-
你不能有两个(点击)事件,只有一个会被触发