简体   繁体   English

在 Angular http 请求后打开 ngx Modal

[英]Open ngx Modal after Angular http Request

I am trying to open an ngx Modal window after an http call made from an angular application.我试图在从角度应用程序进行 http 调用后打开一个ngx 模态窗口。

Here is the code of app.module.ts这是app.module.ts的代码

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { ReactiveFormsModule } from '@angular/forms';
import { AppComponent } from './app.component';
import { HttpClientModule } from '@angular/common/http';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { ModalModule } from 'ngx-bootstrap/modal';
@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    HttpClientModule,
    ReactiveFormsModule,
    BrowserAnimationsModule,
    ModalModule.forRoot(),
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

Here is the code of the component in wich the http call is made这是进行http调用的组件的代码

import { Component, TemplateRef, ViewChild } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { FormControl, FormGroup, Validators } from '@angular/forms';
import { Tour } from './Models/Tour/tour.model';
import { BsModalService, BsModalRef } from 'ngx-bootstrap/modal';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss']
})
export class AppComponent {
  title = 'AnguLearn';
  public fGroup: FormGroup;
  public Tours : Tour[] = [];
  public Empty = false;
  @ViewChild('template', {static: true}) modalr? : TemplateRef<any>;
  modalRef?: BsModalRef;
  constructor(private modalService: BsModalService){
      
  }
  
  public getDatas(){
    
    const httpOptions = {
      headers: new HttpHeaders({
         'Content-Type': 'application/json; charset=utf-8'
      })
   };

  this.http.post<[Tour]>("https://localhost:44387/api/tours", filter_details, httpOptions).subscribe(
      (response)=>{
        this.Tours = response;
        this.Empty = response.length <= 0;
        this.openModal(this.modalr);//I want to open a modal box here
      }, errors=>{ console.log(errors)});
  }

  openModal(template: TemplateRef<any>) {
    this.modalRef = this.modalService.show(template);
  }
}

It did not work, and it did not compile.它不起作用,也无法编译。 I always get the following error on the line with this.openModal(this.modalr);我总是在使用this.openModal(this.modalr); 时遇到以下错误 :

Argument of type 'TemplateRef | 'TemplateRef | 类型的参数undefined' is not assignable to parameter of type 'TemplateRef'. undefined' 不能分配给类型为 'TemplateRef' 的参数。 Type 'undefined' is not assignable to type 'TemplateRef'.类型 'undefined' 不能分配给类型 'TemplateRef'。

Any idea ?任何的想法 ?

You have a type error here.你在这里有一个类型错误。

@ViewChild('template', {static: true}) modalr? : TemplateRef<any>;

? means optional value for modalr .表示modalr可选值。 If it's true, you should change your methods in this way of typing.如果是这样,您应该以这种打字方式更改您的方法。 Or if you sure to have value in TemplateRef - just remove ?或者,如果您确定在TemplateRef有价值 - 只需删除? sign from property syntax like that:从属性语法中签名,如下所示:

 @ViewChild('template', {static: true}) modalr : TemplateRef<any>;

If you not sure you have template value do that:如果您不确定您有模板值,请执行以下操作:

  openModal(template?: TemplateRef<any>) {
    if (template) {
      this.modalRef = this.modalService.show(template);
    }
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM