"Looking to access a model from a TypeScript file in Angular - here's how to

When my page loads, I am attempting to open a modal model. However, despite my efforts, the model does not open. I need to be able to trigger the click() event on the Launch demo modal button from ngInit()

HTML

<ng-template #content let-c="close" let-d="dismiss">
      <div class="modal-header">
        <h4 class="modal-title" id="modal-basic-title">Hi there!</h4>
        <button type="button" class="btn-close" aria-label="Close" (click)="d('Cross click')"></button>
      </div>
      <div class="modal-body">
        <p>Hello, World!</p>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-outline-dark" (click)="c('Save click')">Save</button>
      </div>
    </ng-template>

    <button class="btn btn-lg btn-outline-primary" [hidden]="true" (click)="open(content)" data-target="#varify_warning">Launch demo modal</button>

TS file

ngOnInit(): void 
  {
    document.getElementById("varify_warning")?.click();
  }

Answer №1

Launch ngbModal with a Button Click

In your .ts file, include the following code:

  open(content: any) {
    this.modalService.open(content);
  }

In your .html file, add the button and modal template:

    <button (click)="open(content)">Open Modal</button>
    <ng-template #content let-c="close" let-d="dismiss">
       Insert modal content here
    </ng-template>

Trigger ngbModal on Component Initiation

In your .ts file, add the following:

        @ViewChild("content",{static:true}) content:ElementRef;
        ngOnInit(): void {
            this.modalService.open(this.content);
        }

Include the modal template in your .html file as follows:

    <ng-template #content let-c="close" let-d="dismiss">
       Insert modal content here
    </ng-template>

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

Angular does not update the progress bar

Imagine having a component html with your own customized round progress bar <round-progress #progress [current]="current" </round-progress> The "Current" value represents the percentage. In my TypeScript component, I have written: ...

The "ng2-CKEditor" package is experiencing compatibility issues with TypeScript in Angular 2

Currently, I am in the process of setting up CKEditor in my angular2 application. My backend platform is node.js and for this purpose, I am utilizing the ng2-CKEditor npm module. Below, you can find snippets from respective files. index.html:: <html& ...

Is it possible to release a typescript package without including the ts files in the

I have a Typescript project that needs to be published. All generated JS files are stored in a directory named build, and declaration files in another directory called declaration. I do not want the .ts files to be included in the published version. Can an ...

What issue are we encountering with those `if` statements?

I am facing an issue with my Angular component code. Here is the code snippet: i=18; onScrollDown(evt:any) { setTimeout(()=>{ console.log(this.i) this.api.getApi().subscribe(({tool,beuty}) => { if (evt.index == ...

Modular Angular Component

I'm currently developing a language learning application using Angular 15. With numerous pages and components featuring diverse content, I am exploring the option of allowing users to customize the font type, size, and other display settings for each ...

Generate a configuration file that allows for the reading and storage of modifications

Is there a way to create a configuration file (JSON) on the local file system using JavaScript where I can write and modify data without losing it when the application is restarted? Any suggestions or solutions for this problem? Thank you for your assista ...

How can I verify the existence of a route in Angular 2?

Is there a way to verify the existence of a route within an Angular project without redirecting? For instance, if a user enters http://localhost:4200/#/timestamp in the URL bar and the route for timestamp does not exist in the project, how can this be ch ...

Utilize regular expressions to substitute a specific string of text with an HTML tag

My task is to transform this text: let text = "#Jim, Start editing to see some magic happen!"; into this format: text = "<span style="color:red">#Jim</span>, Start editing to see some magic happen!"; Here is my ...

What is the best approach for ensuring optimal maintainability when components X and Y rely on shared data?

When components X and Y are children of component A and share data obtained from a REST API call, what is the most efficient method or pattern to handle this situation? Considering that the API data is not updated frequently, the ideal approach would invo ...

The parameter type 'Object' cannot be assigned to the parameter type 'string'

Everything seems to be working fine in my application, but I am encountering the following error message: The argument of type 'Object' is causing an issue and cannot be assigned to a parameter of type 'string' Here is the code snip ...

Why won't my boolean value in the Angular service reflect in the HTML code?

For my project, I am utilizing a stepper and I need to display or hide a div based on the selected product. However, I am encountering an issue where the HTML isn't updating when I try to call the done() function. .service public beneficiary = true; ...

Experience the power of React TypeScript where functions are passed as props, but access is restricted

Currently, I am working on creating a toggle button using react and typescript. In order to challenge myself, I have decided to pass a function as a prop to a child component to implement a complex feature. From what I remember, utilizing 'this.props& ...

Creating one-to-one relationships in sequelize-typescript can be achieved by setting up multiple foreign keys

I have a question about adding multiple foreign keys to an object. Specifically, I have a scenario with Complaints that involve 2 Transports. One is used to track goods being sent back, and the other is used for goods being resent to the customer. @Table({ ...

Creating a TypeScript frozen set: A step-by-step guide

Imagine having a group of values that you want to protect from being edited, as shown below: // These values should not be editable. const listenedKeys = new Set(['w', 'a', 's', 'd']) // This value can be accessed w ...

Understanding how to handle POST data from an Angular2 application in PHP

I am trying to establish a connection between my angular2 application and PHP backend. My desired approach is as follows: this.http.post('/users/create', {email: email, password: password}); However, I am encountering an issue where the $_POST ...

Using Firebase with Angular 4 to fetch data from the database and show it in the browser

Currently diving into Angular 4 and utilizing Firebase database, but feeling a bit lost on how to showcase objects on my application's browser. I'm looking to extract user data and present it beautifully for the end-user. import { Component, OnI ...

angular component that takes a parameter with a function

Is there a way for my angular2 component to accept a function as a parameter? Uncaught Error: Template parse errors: Can't bind to 'click' since it isn't a known property of 'input'. (" minlength="{{minlength}}" [ERROR -&g ...

Exploring the depths of routing within a maze of interconnected modules

Currently, I am developing my first Angular 2 application. The routing structure I have set up is as follows - /home /projects/ /projects/:id/members /projects/:id/members/:id/tasks While researching online resources about this topic, I noticed that mo ...

What is the best way to iterate through an enum?

The type 'string' cannot be assigned to the type 'online' or 'offline'. I must be overlooking something, but I'm not sure what it is. enum StatusOptions { ONLINE = 'ONLINE', OFFLINE = 'OFFLINE', ...

Tips for showing data from an hour ago in Angular

Here is the code snippet provided: data = [ { 'name' : 'sample' 'date' : '2020-02-18 13:50:01' }, { 'name' : 'sample' 'date' : '2020-02- ...