question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Closing a modal in the type script - Angular 4

See original GitHub issue

Is there a way that a currently opened modal can be closed inside the type script in Angular 4?

I have a submit button, the submit button has a (click) function, the function inside the type script has an if and else statement inside. I want to perform the closing of a modal inside the else statement. Is it possible?

I am just newbie in Angular, I tried using .hide and .close method, but it doesn’t work. Thanks!

Below is my code:

HTML: encaslogin.component.html

<div class="modal fade" #encasUnPwModal id="encasUnPwModal" data-keyboard="false" data-backdrop="static">
<div class="modal-dialog" style="width: 400px;">   

		 <!-- Modal content -->
  <div class="modal-content wrapper">

		<img src="../assets/images/logo_login.png" style="margin-top: 30px;">

		<div class="input-group" style="margin-top: 10px; margin-left: 50px; margin-right: 50px;">
			<span class="input-group-addon"><i class="glyphicon glyphicon-user"></i></span>
			<input type="text" id="username" class="form-control" #username name="username" placeholder="User" maxlength="50" required>
		</div>

		<div class="input-group" style="margin-top: 10px; margin-left: 50px; margin-right: 50px; margin-bottom: 20px;">
			<span class="input-group-addon"><i class="glyphicon glyphicon-asterisk"></i></span>
			<input type="password" id="password"  class="form-control" #password name="password" placeholder="Password" maxlength="50" required>
		</div>

		<div #encasReturn1 class="text-danger"><strong>{{encasReturn}}</strong></div><br>
			
		<div #encasUsernameReturnDiv>{{encasUsernameReturn}}</div>

		<button type="button" (click)="loadEncasResponse(username.value, password.value);
		username.value=''; password.value=''" class="btn btn-primary btn-md"
			style="margin-bottom: 30px; width: 90px; margin-right: 20px;">OK
		</button>
		
		<button data-dismiss="modal" type="button" class="btn btn-default btn-md"  
			(click)="clearFields(); username.value='';password.value=''"
			style="margin-bottom: 30px; width: 90px;">Cancel
		</button>

	</div>

 </div>
 </div>

Type Script: encaslogin.components.ts

import { encasAuthUrl, Constants } from ‘./…/…/core/constants’;

import { Component, OnInit, Injectable, Input, Output, EventEmitter, ViewChild, ElementRef } from ‘@angular/core’; import { FormBuilder } from ‘@angular/forms’; import { NgForm } from ‘@angular/forms’; import { Location } from ‘@angular/common’; import { Http, Response } from ‘@angular/http’; import ‘rxjs/add/operator/map’; import { HomeComponent } from ‘…/…/home/home.component’;

@Component({ selector: ‘app-encaslogin’, templateUrl: ‘./encaslogin.component.html’ // styleUrls: [‘./home.component.css’] })

export class EncasLoginComponent { encasAuthUrl = encasAuthUrl; profile = {response: ‘’, message: ‘’}; encasUsernameReturn = ‘’; encasReturn = ‘’;

constructor( private location: Location, private homeComponent: HomeComponent, private http: Http ) {}

loadEncasResponse(username, password, encasUnPwModal) { if (username === ‘’ || password === ‘’) { this.encasReturn = ‘Username and Password are required fields.’; this.encasUsernameReturn = ‘’; } else if (username.trim() === ‘’ || password.trim() === ‘’) { this.encasReturn = ‘Whitespace(s) are not allowed as input.’; this.encasUsernameReturn = ‘’; } else { this.http.get(encasAuthUrl + username + / + password) .map((response: Response) => response.json()) .subscribe(data => {this.profile = data; if (this.profile.response === ‘success’) { this.encasUsernameReturn = this.profile.message; this.encasReturn = ‘’; // I WANT TO PUT THE CLOSING OF THE MODAL HERE. } else { this.encasUsernameReturn = ‘’; this.encasReturn = this.profile.message } }); } }

clearFields() { this.encasReturn = ‘’; this.encasUsernameReturn = ‘’; }

}

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:15 (2 by maintainers)

github_iconTop GitHub Comments

6reactions
dhaval-phemesoftcommented, Mar 28, 2018

-> component.html file <a id=“linkid” (click)=“openmodel(.id)” data-toggle=“modal” data-target=“#myModalcostcase”>Open model /* Open model link */

-> component.ts File document.getElementById(‘linkid’).click(); // wher u want to close your model Best of luck…

2reactions
harshadp37commented, Dec 11, 2018

I already find a way to close the modal inside the type script, but through jQuery: jQuery(‘#encasUnPwModal’).modal(‘hide’);

I’m getting jQuery is undefined

put this after import & before @Component : declare var jQuery:any;

Read more comments on GitHub >

github_iconTop Results From Across the Web

Angular 5 Modal close programmatically - Stack Overflow
This is a modal. After completing the task the modal do, I want to close it programmatically with typescript. I am trying to...
Read more >
Angular Close Bootstrap Modal - StackBlitz
Starter project for Angular apps that exports to the Angular CLI. ... <h2>Modal Close</h2>. <!-- Trigger the modal with a button -->.
Read more >
how cen i close modal from .ts file?
In your modal's HTML -- button type="button" mdbBtn color="primary" class="waves-light ... aria-label="Close" (click)="closeModal()" mdbWavesEffect>Close.
Read more >
Angular 8, 7, 6, 5, 4, 2 - Open and Close Modal Popup Using ...
Open Modal Popup Using Typescript and Bootstrap – ... After clicking the button open the modal dialog using open/close event binding methods ...
Read more >
Angular 8 - Custom Modal Window / Dialog Box
To open a modal call the modalService.open() method with the id of the modal you want to open, e.g. modalService.open('custom-modal-1') ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found