【问题标题】:ng-bootstrap alerts won't closeng-bootstrap 警报不会关闭
【发布时间】:2020-05-12 16:44:27
【问题描述】:

当我点击 X 按钮时,我的提醒不会关闭。

我使用 Angular cli 创建了一个新应用。我按照说明here 加载引导程序:

npm install bootstrap@4.0.0-alpha.6  

然后我按照指示here 加载 ng-bootstrap:

npm install --save @ng-bootstrap/ng-bootstrap

app.module.ts:

import { NgbModule } from '@ng-bootstrap/ng-bootstrap';

@NgModule({
  declarations: [
    AppComponent,
    NavComponent
  ],
  imports: [
    BrowserModule,
    FormsModule,
    HttpModule,
    NgbModule.forRoot()
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

app.component.ts

import { Component } from '@angular/core';


@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'Testing...';
}

app.component.html

<app-nav></app-nav>
<ngb-alert>Alert</ngb-alert>
...

警报显示出来,并且样式正确,但是当我单击 X 时它没有关闭。其他组件正在工作(选项卡集、下拉菜单)。我在 app.component.ts 中遗漏了什么吗?

【问题讨论】:

    标签: ng-bootstrap


    【解决方案1】:

    你必须在你的组件中定义一个close事件方法。

    HTML:

    <ngb-alert *ngIf="!closed" (close)="closed=true">Alert</ngb-alert>
    

    组件:

    import { Component } from '@angular/core';
    
    @Component({
      selector: 'app-root',
      templateUrl: './app.component.html',
      styleUrls: ['./app.component.css']
    })
    export class AppComponent {
      title = 'Testing...';
      closed = false;
    }
    

    希望对你有帮助。

    【讨论】:

    • 简单而伟大的回答尊重,根据需求定义变量显示和隐藏警报
    【解决方案2】:

    这很晚,但可能会帮助其他人寻找简单的可关闭警报。该组件封装了 Devansh 的解决方案:

    import { Component, Input } from '@angular/core';
    
    @Component({
        selector: 'app-alert',
        template: '<ngb-alert [type]="type" *ngIf="open" (close)="open=false"><ng-content></ng-content></ngb-alert>',
    })
    export class InfoPanelComponent {
        public open = true;
        @Input() type = 'info';
    }
    

    用法:

    <app-alert>I'm a closeable alert!</app-alert>
    

    【讨论】:

      猜你喜欢
      • 2015-07-01
      • 1970-01-01
      • 2013-12-27
      • 1970-01-01
      • 2017-03-06
      • 1970-01-01
      • 2014-04-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多