angular - 在具有按钮的标题上设置条件(禁用/显示按钮)[Angular]

标签 angular

我的项目中有一个标题。标题中包含一个按钮。 现在我的项目中有一个新组件和一个新模块,我想使用相同的 header 但没有按钮。我怎样才能做到这一点?

我的标题 HTML

<header>
    <div class="container">
        <button mat-fab class="mat-success" [mdePopoverTriggerFor]="appPopover" mdePopoverTriggerOn="click" mdePopoverPositionX="before">+</button>
        <mde-popover #appPopover="mdePopover" [mdePopoverOverlapTrigger]="false" [mdePopoverCloseOnClick]="true">
            <app-reports-list></app-reports-list>
        </mde-popover>
    </div>
</header>

在我的原始组件中:

<app-header></app-header>
<main>
    <app-reports></app-reports>
</main>
<app-footer></app-footer>

在我的新组件中(这里我希望它没有按钮,只有标题)

<app-header></app-header>
<main>
    <app-kit-process></app-kit-process>
</main>
<app-footer></app-footer>

谢谢,祝你编码日愉快:)

最佳答案

您可以在服务中放置一个可以全局使用的变量,并使用它来禁用和启用按钮。也就是说,当您想要禁用该按钮时,可以在该组件的 ngOninit 中将变量设置为“true”。

示例代码 service.ts

import { Injectable } from '@angular/core';
@Injectable()
export class dataService {
 showbutton: boolean = true;}

示例代码component_1.ts

import { Component, OnInit} from '@angular/core';
import { dataService } from '../service/data.service';
@Component( {
    selector: 'app-component_1',
    templateUrl: './component_1.component.html',
    styleUrls: ['./component_1.component.css']
} )
export class component_1 {
    constructor( public bookmarkRoot: dataService, private router: Router){}
    ngOnInit() {
       this.bookmarkRoot.showbutton=false  //hide/disable button
      }
}

示例代码component_2.ts

import { Component, OnInit } from '@angular/core';
import { dataService } from '../service/data.service';
@Component( {
    selector: 'app-component_2',
    templateUrl: './component_2.component.html',
    styleUrls: ['./component_2.component.css']
} )
export class component_2 {
    constructor( public bookmarkRoot: dataService, private router: Router){}
    ngOnInit() {
       this.bookmarkRoot.showbutton=true//hide/disable button
      }
}

标题 HTML

<header>
  <div class="container">
    <button mat-fab class="mat-success" [mdePopoverTriggerFor]="appPopover" mdePopoverTriggerOn="click" mdePopoverPositionX="before" [disable]="bookmarkRoot.showbutton">+</button>
    <mde-popover #appPopover="mdePopover" [mdePopoverOverlapTrigger]="false" [mdePopoverCloseOnClick]="true">
      <app-reports-list></app-reports-list>
    </mde-popover>
  </div>
</header>

您还必须在 header.component 中导入数据服务。您可以仅从 stackover flow 找到如何使用服务在组件之间进行通信

关于angular - 在具有按钮的标题上设置条件(禁用/显示按钮)[Angular],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58213392/

相关文章:

javascript - 在 Stackblitz 上使用 HttpClient 读取 data.json?

Angular Leaflet 弹出式点击不起作用?

angular - 如何通过管道传输系列同步。和异步。在 Angular 服务中使用 RxJs 运算符执行任务

Angular - 将管道作为变量传递

arrays - 在 typescript 中遍历数组

单击路线时 Angular2 应用程序卡住

angular - 无法在本地主机上的Docker容器之间进行通信

angular - 我正在使用 *ngFor 来迭代卡片布局中的元素。我想随机改变他们的背景颜色有什么办法吗?

Bootstrap 中的 Angular2 提供程序与 @component

组件中管道过滤器的 angular2 错误