gpt4 book ai didi

javascript - 将 boolean 值传递给另一个 Angular 组件

转载 作者:行者123 更新时间:2023-12-04 15:44:04 24 4
gpt4 key购买 nike

我有两个 Angular 组件

results.table 和results.query

我想在用户单击 results.query.component 中的重置按钮时将表隐藏在 results.table.component 中

也许我对事件发射器做错了,或者也许有更好的方法来做到这一点

results.table HTML

<div *ngIf='results?.length>0'>
<table *ngIf="showResults" class='table'>
<tr>
<th>Result Name</th>
<th>Location</th>
</tr>
<tbody>
<ng-template ngFor let-results [ngForOf]='items' let-i="index">
<tr>
<td>
<span>{{result?.description}}</span>
</td>
<td>
<span>{{result?.location}}</span>
</td>
</tr>
</ng-template>
</tbody>
</table>
</div>

results.table TS

showResults: boolean = true;

showResults(event) {
console.log('this is not getting called')
if (event) {
this.showResults = false;
}
}

results.query HTML

<div class="panel-body">
<form (submit)="onSubmitClicked()">
<div class="row">
<div class="form-group col-md-12 col-xs-12">

<div class="col-xs-12 col-sm-3 col-md-3 col-lg-3">
<label class="col-md-12 col-xs-12 control-label no-margin no-padding">Location: </label>
<pg-radio-toggle-select class="col-md-12 col-xs-12 no-margin no-padding" name="locationChangeInput" [(ngModel)]="Location"
(selectedChanged)="onFilteringLocation($event)" [options]='locationOptions'>
</pg-radio-toggle-select>
</div>

<pg-inputfield name="description" class="col-xs-12 col-sm-3 col-md-3 col-lg-3" [(ngModel)]="paramsModel.description"
displaytext="Name:"></pg-inputfield>
</div>
</div>

<div>
<button type="reset" class="btnReset" (click)="reset()">Reset</button>
<button type="submit" name="btnSearch">Search</button>
</div>
</form>
</div>

results.query TS

import {Component, OnInit, EventEmitter, Output} from '@angular/core';
import * as _ from 'lodash';
import { LocationService } from '../location-service.service';

@Component({
selector: 'result-query',
templateUrl: './result-query.component.html',
styleUrls: ['./result-query.component.less'],
})
export class ResultQueryComponent implements OnInit {
@Output() showResults: EventEmitter<boolean> = new EventEmitter<boolean>();

constructor(
private LocationService: LocationService,
) {
this.reset();
}

ngOnInit() {
this.reset();
}

onSubmitClicked() {
console.log('test')
}

reset(): void {
console.log('I am the reset king');
this.showResults = false;
this.showResults.emit(true);
this.onSubmitClicked();
}
}

最佳答案

如果两个组件确实有父子关系,您可以使用@Input() @Output() 装饰器。

4 Ways to share data between angular components

Component Interaction

Input Output Example

父组件

import { Component, OnInit, ViewEncapsulation } from '@angular/core';
import { Stephen } from '../stephen.model';

@Component({
selector: 'app-parent',
template: `

Hello, Mr. (or Ms.): {{ selectedName }}

`,
styleUrls: ['./parent.component.css'],
encapsulation: ViewEncapsulation.None
})

export class ParentComponent implements OnInit {
stephen: Stephen;
selectedName: string;

constructor() {
this.stephen = new Stephen();
this.selectedName = this.stephen.firstName;
}

ngOnInit() {
}

updateName(selectedName: string): void {
console.log('in parent');
this.selectedName = selectedName;
}

}

子组件

import { Component, OnInit, ViewEncapsulation, Input, Output, EventEmitter } from '@angular/core';
import { Stephen } from '../../stephen.model';
@Component({
selector: 'app-child',
template: `
{{ stephen.firstName }}
{{ stephen.lastName }}
{{ stephen.fullName }}
`,
styleUrls: ['./child.component.css'],
encapsulation: ViewEncapsulation.None
})
export class ChildComponent implements OnInit {
@Input() stephen: Stephen;
@Output() onNameSelected: EventEmitter;
constructor() {
this.onNameSelected = new EventEmitter();
}
ngOnInit() {
}
clicked(name: string): void {
this.onNameSelected.emit(name);
}
}

重要 - 第二种解决方案

但在您的情况下,这两个组件似乎没有父子关系。如果你想在两个组件之间共享数据,你可以创建一个可共享的服务。该服务将包含 EventEmitter,需要最新更改的组件将在 ngOnInit 方法中订阅该组件,而具有最新数据的组件将调用该可共享服务的函数以发出该事件。

可共享服务

import { Injectable, Output, EventEmitter } from '@angular/core';

@Injectable({
providedIn: 'root'
})
export class MessengerService {

@Output() change: EventEmitter<any> = new EventEmitter();

sendData(data: any): any {
this.change.emit(data);
}

}

想要了解此更改的组件将像这样在 ngOnInit 中订阅此事件。

messengerService.change.subscribe(emitedValue => {
this.value = emitedValue;
});

具有新更改的组件将调用 sendData 方法是消息/可共享服务,以在需要时将新数据发布到事件订阅者。

关于javascript - 将 boolean 值传递给另一个 Angular 组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56653607/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com