Encountering a problem while attempting to integrate a modal using ng2-bootstrap

I encountered an issue while trying to incorporate a modal using ng2-bootstrap. The error message displayed was 'Error: ApplicationRef instance not found', and I am unsure of how to resolve it.

After researching, I found that for ng2-bootstrap, you need to apply a workaround. I attempted to implement it in app.component.ts but unfortunately, it did not solve the problem.

https://i.sstatic.net/dFzae.png

add-domain.component.html

<!-- Large modal -->
<button class="btn btn-primary" (click)="lgModal.show()">Large    modal</button> 

<div bsModal #lgModal="bs-modal" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel" aria-hidden="true">
  <div class="modal-dialog modal-lg">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" (click)="lgModal.hide()" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
        <h4 class="modal-title">Large modal</h4>
      </div>
      <div class="modal-body">
        ...
      </div>
    </div>
  </div>
</div>

add-domain.component.ts

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

@Component({
    selector: 'add-domain',
    templateUrl: 'add-domain.component.html',
})

export class AddDomainComponent {
  private viewContainerRef: ViewContainerRef;

  public constructor(viewContainerRef: ViewContainerRef) {
    // A small hack is needed to access the application root view container ref
    this.viewContainerRef = viewContainerRef;
      this.title = "Ajouter un domaine"
  }
}

Answer №1

Check out their latest trick, you'll need to include the viewContainerRef:

class MainApp {
  private viewContainerRef: ViewContainerRef;

  public constructor(viewContainerRef:ViewContainerRef) {
    // This simple trick is necessary to access the application's root view container reference
    this.viewContainerRef = viewContainerRef;
  }
}

Answer №2

Encountered a similar problem, but managed to resolve it by modifying my main component as shown below:

// Solution for addressing modal error
ComponentsHelper.prototype.getRootViewContainerRef = function () {
    // Referring to: https://github.com/angular/angular/issues/9293
    if (this.root) {
        return this.root;
    }
    var comps = this.applicationRef.components;
    if (!comps.length) {
        throw new Error("ApplicationRef instance not found");
    }
    try {
        /* Another workaround implemented, refer to the mentioned issue for more information */
        var rootComponent = this.applicationRef._rootComponents[0];
        //this.root = rootComponent._hostElement.vcRef;
        this.root = rootComponent._component.viewContainerRef;
        return this.root;
    }
    catch (e) {
        throw new Error("ApplicationRef instance not found");
    }
};

@Component({
    selector:       'intro',
    template:       'template.html',
    providers:    [MyDataService]
})

Dive into the details and discussion related to this issue here ==> https://github.com/valor-software/ng2-bootstrap/issues/986

Answer №3

Give this code a shot. I implemented it and it solved the issue for me. Just set the backdrop to false.

<div class="modal fade" bsModal #testModal="bs-modal" [config]="{backdrop: false}" ...>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Tips for retrieving the generated ID from the server immediately following form submission using the Post method in TypeScript

When submitting a long-form, it is important to ensure that no data is lost. Users should be able to stay on the same page to make any necessary changes after clicking the submit button. I need to receive the unique id generated by the server upon submissi ...

Stop SCSS from processing CSS variables in Angular-CLI

I am currently using Angular5 with sass v1.3.2. My goal is to dynamically change a color that is widely used in the scss files of my single page app without having to recompile new files. This color is globally defined in my _variables.css as: $brand: # ...

Despite setting the esModuleInterop flag, I am still encountering an error with default imports

My React project with TypeScript is causing some issues. In the main tsx file, the import React from 'react' line works fine. However, in my test file, I keep getting the TS1259 error. I suspect there might be a problem with my TS/Jest/Babel conf ...

Next.js Custom App now offers full support for Typescript in Accelerated Mobile Pages (

I am looking to implement AMP in my custom Next.js project using Typescript. While the official Next.js documentation does not offer support for Typescript, it suggests creating a file called amp.d.ts as a workaround. My application includes a src folder ...

"Exploring the Angular 3 router's wildcard route matching feature

Why does the following route configuration always navigate to ** instead of the route for app/jungle? import {bootstrap} from '@angular/platform-browser-dynamic'; import { RouterConfig, provideRouter } from '@angular/<a href="/cdn-cgi/ ...

Using TypeScript to Add Items to a Sorted Set in Redis

When attempting to insert a value into a sorted set in Redis using TypeScript with code like client.ZADD('test', 10, 'test'), an error is thrown Error: Argument of type '["test", 10, "test"]' is not assigna ...

Is there a way to include lazy loaded components in the production dist folder using angular-cli bundling?

For my development using angular-cli, I have executed the following commands and codes to construct my project. npm install angular-cli (angular-cli: 1.0.0-beta.10) ng new my-app ng g component lazy-me Then, I included a file named app.router.ts with t ...

"Handling dependency injection with cyclic dependencies along with a custom implementation of HTTP and ConfigService

I am currently working on implementing a ConfigService to retrieve the appropriate configuration for each environment within the project. However, I have run into an issue with cyclic dependencies. (index):28 Error: (SystemJS) Provider parse errors: C ...

Is there an Angular counterpart to Vue's <slot/> feature?

Illustration: Main component: <div> Greetings <slot/>! </div> Subordinate Component: <div> Planet </div> Application component: <Main> <Subordinate/> </Main> Result: Greetings Planet! ...

Choose between creating an observable pipe within a function or storing it in a variable

Currently, I have a functional code snippet that leverages the Angular service to create an Observable pipeline. This pipeline utilizes operators like mergeMap, filter, map, and shareReplay(1) to manage user authentication and fetch the onboarding status f ...

developing customized designs in an Angular 2 application

I have been working on developing a basic application in angular 2. The layout of my home page consists of a top navigation bar and a side navigation bar. Here is the code snippet for my homepage template: home.component.html <div class="well sideba ...

Transfer your focus to the following control by pressing the Enter key

I came across a project built on Angular 1.x that allows users to move focus to the next control by pressing the Enter key. 'use strict'; app.directive('setTabEnter', function () { var includeTags = ['INPUT', 'SELEC ...

Advantages of using ConfigService instead of dotenv

What are the benefits and drawbacks of utilizing @NestJS/Config compared to using dotenv for retrieving environment variables? Although I can create a class to manage all envvars in both scenarios, is it necessary? I am aware that @NestJS/Config relies on ...

Exploring the Possibilities with Material UI v4: Expanding on PaletteColor

Looking to make a simple adjustment in Material UI v4's (v4.11) palette, specifically adding a new field to PaletteColorOptions like darker?: string. The type definition can be found in the relevant Material UI module: createPalette.d.ts export type ...

Error in JavaScript: A surprise anonymous System.register call occurred

Within Visual Studio 2015, there exists a TypeScript project featuring two distinct TypeScript files: foo.ts export class Foo { bar(): string { return "hello"; } } app.ts /// <reference path="foo.ts"/> import {Foo} from './f ...

Building a React Redux project template using Visual Studio 2019 and tackling some JavaScript challenges

Seeking clarification on a JavaScript + TypeScript code snippet from the React Redux Visual Studio template. The specific class requiring explanation can be found here: https://github.com/dotnet/aspnetcore/blob/master/src/ProjectTemplates/Web.Spa.ProjectT ...

Dynamically importing TypeScript interfaces for React code splitting

Is it possible to utilize dynamic import('path') for an exported interface? ...

Unable to transfer a callback function from SocketIO to typescript

My server is built with nodeJS and Typescript utilizing SocketIO for an online chat application. However, I am facing difficulties in transferring the callback function provided by TypeScript library. Can someone guide me on how to correctly call the call ...

What is the technique for passing a value from parent to child components in React?

Here is a plunker demonstration that I have set up: https://plnkr.co/edit/5XbuxoGG6NqomayLP4Ae?p=preview Below is the code snippet: > @Component({ selector: 'my-app', template: ` > <layout [child]="childModal"> > &l ...

Trouble encountered when attempting to call a function within another function in StencilJS

I am currently following a tutorial on building a drag and drop file uploader using StencilJS for some practice and fun. However, I have encountered an error in the code. Below is a snippet of the code, but I can provide more if necessary. @Component({ ...