angular - 子组件中的按钮不显示
问题描述
如下所示,我有一个名为的组件Treatment-as-tiff
,在它的 html 文件中应该有一个按钮。当特定操作发生时,该按钮应该可见。site-map-component
问题是当我运行代码并将该操作设置为发生时,按钮不显示
请让我知道我在这里缺少什么
TreatmentAsTIFF html:
<button *ngIf="iTreatmentAsTIFFVisibilityPasser.visibilityState"
title="Get Treatment As TIFF"
class="btn btn-sm btn-icon"
(click)="getTreatmentAsTIFF()">
{{ "SITE.GET_TREATMENT_AS_TIFF" | translate }}
<ng-content></ng-content>
</button>
TreatmentAsTIFF.ts:
import { ThrowStmt } from '@angular/compiler';
import { Component, OnInit } from '@angular/core';
import { SYNOPSServicesProviderService} from '../services/SYNOPSServiceProvider/synopsservices-provider.service'
import { Subscription } from 'rxjs';
export interface ITreatmentAsTIFFVisibilityPasser {
visibilityState: boolean
}
@Component({
selector: 'app-treatment-as-tiff',
templateUrl: './treatment-as-tiff.component.html',
styleUrls: ['./treatment-as-tiff.component.css']
})
export class TreatmentAsTIFFComponent implements OnInit {
iTreatmentAsTIFFVisibilityPasser:ITreatmentAsTIFFVisibilityPasser;
subscriptionEvtEmitterOnTratmentAsTIFFEmitted: Subscription;
constructor(private synopsServicesProvider:SYNOPSServicesProviderService) {
this.iTreatmentAsTIFFVisibilityPasser = {} as ITreatmentAsTIFFVisibilityPasser;
this.iTreatmentAsTIFFVisibilityPasser.visibilityState = false
}
ngOnInit(): void {
this.subscriptionEvtEmitterOnTratmentAsTIFFEmitted = this.synopsServicesProvider.getEventEmitterResponseFoRiskCalculation().subscribe((response: object)=> {
console.log("OnTratmentAsTIFF response received: ",response)
});
}
public getVisibilityState() {
return this.iTreatmentAsTIFFVisibilityPasser.visibilityState;
}
public setInvisible() {
this.iTreatmentAsTIFFVisibilityPasser.visibilityState = false
}
public setVisible() {
this.iTreatmentAsTIFFVisibilityPasser.visibilityState = true
}
private getTreatmentAsTIFF(fieldGeometry):void{
this.synopsServicesProvider.startWebServiceForGetTreatmentAsTIFF(fieldGeometry);
}
}
站点地图组件:
import { TreatmentAsTIFFComponent } from '../treatment-as-tiff/treatment-as-tiff.component';
export interface ITreatmentAsTIFFVisibilityPasser {
visibilityState: boolean
}
iTreatmentAsTIFFButtonVisibilityPasser:ITreatmentAsTIFFVisibilityPasser;
treatmentAsTIFFComponent: TreatmentAsTIFFComponent;
constructor() {
this.iTreatmentAsTIFFButtonVisibilityPasser = {} as ITreatmentAsTIFFVisibilityPasser;
this.iTreatmentAsTIFFButtonVisibilityPasser.visibilityState = false
}
...
...
...
private toggleGetTreatmentAsTIFFButtonToVisible():void{
this.treatmentAsTIFFComponent.setVisible()
}
private toggleGetTreatmentAsTIFFButtonToInvisible():void{
this.treatmentAsTIFFComponent.setInvisible()
}
站点地图组件.html:
<app-treatment-as-tiff></app-treatment-as-tiff>
解决方案
这是因为您将in设置visibilityState
为false。如果要显示它,请将其设置为 true:iTreatmentAsTIFFVisibilityPasser
TreatmentAsTIFFComponent
export class TreatmentAsTIFFComponent implements OnInit {
...
constructor(private synopsServicesProvider:SYNOPSServicesProviderService) {
this.iTreatmentAsTIFFVisibilityPasser = {} as ITreatmentAsTIFFVisibilityPasser;
this.iTreatmentAsTIFFVisibilityPasser.visibilityState = true; // <-- HERE!
}
...
}
或者你没有toggleGetTreatmentAsTIFFButtonToVisible
在SiteMapComponent
. 而且我确定您对TreatmentAsTIFFComponent
inside子元素的引用SiteMapComponent
不起作用。
以下是获取TreatmentAsTIFFComponent
inside参考的方法SiteMapComponent
:
import {
...
ViewChild,
...
} from '@angular/core';
...
@Component({
...
})
export class SiteMapComponent implements OnInit {
@ViewChild(TreatmentAsTIFFComponent) treatmentAsTIFFComponent: TreatmentAsTIFFComponent;
...
private toggleGetTreatmentAsTIFFButtonToVisible():void{
this.treatmentAsTIFFComponent.setVisible()
}
private toggleGetTreatmentAsTIFFButtonToInvisible():void{
this.treatmentAsTIFFComponent.setInvisible()
}
}
推荐阅读
- android - unity google play 服务和 admob 应用程序在打开时崩溃
- php - Wordpress 在特定页面中隐藏新编辑器
- angular - 如何更改 Angular 6 中组件的状态?
- javascript - 使用 jquery 创建 json 数据时同样的关键问题
- python - VSCode - 在终端和 pythonpath 中运行 Python 文件
- java - 如何在简单的 Java 图像查看器中显示 BMP 图像
- ruby-on-rails - 使用 Rails RSpec 和 Capybara 和 Selenium 我如何测试以确保显示模式
- java - 更新架构时,当前线程的范围“会话”不活动
- c++ - Windows WaitCommEvent 等待 n 个字符
- python-3.x - 如何在 Scrapy 中运行多个相同的蜘蛛?