首页 文章

ng-bootstrap警报不会关闭

提问于
浏览
1

单击X按钮时,我的警报不会关闭 .

我使用angular cli来创建一个新的应用程序 . 我按照说明here加载bootstrap:

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时它不会关闭 . 其他组件正在工作(tabset,下拉列表) . 我在app.component.ts中遗漏了什么吗?

1 回答

  • 8

    您必须在组件中定义 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;
    }
    

    我希望这能帮到您 .

相关问题