Activating Modal in Angular 5 when Form is Submitted

I am currently integrating Angular 5 and Bootstrap 3 for a project. One of the forms on my website sends the input field data via email and includes a modal. I want to be able to trigger the modal based on a successful or error response from the server. I attempted to use *ngIf to trigger the modal but it did not work as expected. Here is a snippet of the HTML code:

<div class="container">
  <form>
    <div>
      <input></input> 
    </div>
  </form>

<div *ngIf="emailSuccess" #modalInfo class="modal fade" id="modal">
modal body
</div>

Below is a portion of my .ts file:

sendEmail(data) {
    this.appService.sendEmail(data.myForm.value).subscribe((data) => {
      console.log('data', data);
      if (data.success) {
        this.emailSuccess = true;
        this.messageHeader = 'Thank You';
        this.messageBody = 'We received your email';
        this.myForm.reset();
      } else if (data.error) {
        this.emailSuccess = false;
        this.messageHeader = 'Error';
        this.messageBody = 'Something went wrong. Please, check your form and try again';
      }
    });
  }

sendEmail() is triggered by:

<button type="button" (click)="sendEmail(this)" class="btn btn-primary " [disabled]="!myForm.valid">Submit</button>

Answer №1

To ensure that a modal is displayed regardless of whether the send operation was successful or encountered an error, I needed to connect a modal trigger to the Submit button.

Previous code:

<button type="button" (click)="sendEmail(this)" class="btn btn-primary " [disabled]="!myForm.valid">Submit</button>

Updated code:

<button type="button" (click)="sendEmail(this)" class="btn btn-primary" data-toggle="modal" href="#modal" [disabled]="!maintenanceForm.valid">Submit</button>

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

In TypeScript, make sure to verify the type of an object to avoid any potential compilation errors stemming

Here is the code snippet: export default class App { el: HTMLElement; constructor(el: string | HTMLElement) { if (typeof el === "string") { this.el = document.getElementById(el); } if (typeof el === typeof this.el) { t ...

Resolve routing problems with lazy loading in Angular7

I have been working on implementing lazy loading in my Angular project. However, I am encountering the same error even after following the suggested solution. Lazy loading error on StackOverflow I have exported the components from the project module and im ...

Avoid navigating to the subscribe block when the server sends a response in Angular

When trying to send a request to the server and check the response, I am not seeing any results. The code for sending the request is below: SendVerificationInfo(item: SendVerificationModel): Observable < any > { return this.httpClient.post < any ...

Jest Matcher issue: the value received must either be a promise or a function that returns a promise

As a dedicated practitioner of TDD, I am currently working on implementing an Exception in my code. Below is the test code I have written: it.each([[{ id: '', token: '', skills: [''] }, 'Unknown resource']])( ...

Issue transferring information between non-related components in an Angular 8 application unrelated to BehaviorSubject

Encountering an issue with passing data between unrelated components using Services and BehaviorSubject. The problem arises when the data is received, as the value of the variable Behavior arrives empty (""), despite the components having no apparent conne ...

How can GraphQL facilitate JOIN requests instead of multiple sequential requests?

I am working with two GraphQL types: type Author { id: String! name: String! } type Book { id: String! author: Author! name: String! } In my database structure, I have set up a foreign key relationship within the books table: table authors (e ...

Neither the child nor the parent are showing the ng-content

I'm working on a project with a child-parent structure and I have the following components: <app-parent> parent component <ng-content select=["parent"]></ng-content> <app-child></app-child> </app-par ...

What is the best way to implement a side navigation bar with 100 tabs using Angular 4?

I am new to learning AngularJS and could use some guidance. Currently, I am working on an Angular 4 SPA project that consists of a sidebar with 100 tabs, each containing different content. Here is an image of the sidebar. I have tried a simple approach by ...

How do I go about mocking a function from a third-party nodejs module when using TypeScript with jest?

I need help with mocking a function from a third-party node module in jest, specifically the fs.readFileSync() function. I have come across several examples online, but I haven't found one that incorporates TypeScript. To illustrate my question, I hav ...

React is inferring the type of the 'charts' property in the object literal as 'any[]'

ide: vscode typescript: 2.7.1 react: 16.3.0-alpha.1 interface IState { numbers: number[]; } class CustomCanvas1 extends React.Component<undefined, IState> { constructor(properties: undefined) { super(properties); this.state = { ...

The const keyword is not automatically inferred as a const when using conditional types for generic type parameters

Within Typescript, the const modifier can be applied to function type parameters. This ensures that the inferred type matches the literal received with as const. function identity<const T>(a: T){ return a } For example, when using identity({ a: 4 ...

What could be the reason for the absence of Mock Service Worker in a React project that has Typescript enabled?

After attempting to integrate Mock Service Worker into my React project with Typescript support, I encountered errors when running the npm install msw --save-dev command. The terminal displayed the following messages: PS F:\Programming\React Prac ...

Count of items in filtered data displayed using Angular 5 *ngFor

Looking for help with my ngFor loop: *ngFor="let item of items | filterBy: ['SN', 'Name']: userFilter | orderBy: order | paginate: {itemsPerPage: 10, currentPage: 1}; let i = index;" Trying to retrieve the count of filtered items. Whe ...

Issue in Angular when using a shared module: displaying "appears in XXX.module but itself has errors" message

In my Ionic v6 project with Angular, I have developed multiple directives that are needed across various pages. To manage these directives effectively, I have created a shared module: import { MediaPage } from './../directivas/media/media.page'; ...

Retrieve ag grid from TypeScript file

I am currently utilizing ag-grid-angular in my Angular application to showcase data. I have a button that is located outside the ag grid, and when it is clicked, I need to retrieve all row data from the grid. I am aware that there is an API available for a ...

Using props in React can be declared either as a `type` or an `interface`

I am working with React code export default class MyComponent extends Component<Props,State> I'm trying to figure out whether I should define my props using a type or an interface. type Props = { isActive: Boolean, onClick: Function } ...

Closures are like the "use" keyword in PHP or the capture list in C++, but they play a similar role in JavaScript and other transpiler languages

PHP has a useful feature with the use keyword, which allows for the usage of 'external' variables in closures. For example: $tax = 10; $totalPrice = function ($quantity, $price) use ($tax){ //mandatory 'use' return ($price * $quan ...

Accessing information independent of Observable data in TypeScript

When attempting to send an HttpRequest in Typescript, I encountered an issue where the received data could not be stored outside of the subscribe function. Despite successfully saving the data within the subscribe block and being able to access it there, ...

Creating your own custom operator using observables is a powerful way

const apiData = ajax('/api/data').pipe(map((res: any) => { if (!res.response) { console.log('Error occurred.'); throw new Error('Value expected!'); } return res.response; }), An enhancement is needed to encapsulate the ...

Webpack 5 lacks compatibility with polyfills for Node.js

I'm facing a challenge with my npm package that is compatible with both Angular and Node.js environments. Recently, I began using the package in Angular v12 projects, but encountered errors like: BREAKING CHANGE: webpack < 5 used to include polyf ...