Ngx-bootstrap: Closing a modal in the type script - Angular 4

Created on 11 Aug 2017  路  15Comments  路  Source: valor-software/ngx-bootstrap

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


Most helpful comment

-> component.html file
Open model /* Open model link */

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

All 15 comments

First of all, get access to a modal.
@ViewChild('encasUnPwModal') public modal: ModalDirective;
then, put this.modal.hide() instead of // I WANT TO PUT THE CLOSING OF THE MODAL HERE.
This should work.

Upon adding this to my code:
@ViewChild('encasUnPwModal') public modal: ModalDirective;

public modal: ModalDirective; is having red lines.

Do I need to include more imports above?


Here is the error logs:

ERROR in D:/APSKED/APSKED_WEBAPP_08032017/src/app/home/encaslogin/encaslogin.component.ts (18,36): Declaration expected.

ERROR in D:/APSKED/APSKED_WEBAPP_08032017/src/app/home/encaslogin/encaslogin.component.ts (18,37): Unused label.

ERROR in D:/APSKED/APSKED_WEBAPP_08032017/src/app/home/encaslogin/encaslogin.component.ts (18,44): Cannot find name 'ModalDirect
ive'.

import { ModalDirective } from 'ngx-bootstrap/modal';
or import { ModalDirective } from 'ngx-bootstrap'; if you're using SystemJS

I tried putting the import.

The error "Cannot find name ModalDirect" is already resolved, but the remaining 2 error still occurs.

ERROR in D:/APSKED/APSKED_WEBAPP_08032017/src/app/home/encaslogin/encaslogin.component.ts (18,36): Declaration expected.

ERROR in D:/APSKED/APSKED_WEBAPP_08032017/src/app/home/encaslogin/encaslogin.component.ts (18,37): Unused label.

I already resolved the 2 issues above, but upon testing it, the modal doesn't close.

There is no error when compiling and running, but when I view the Console log in Inspect Element on the browser. Here is the error:

ERROR TypeError: _this.modal.hide is not a function
Stack trace:
../../../../../src/app/home/encaslogin/encaslogin.component.ts/EncasLoginComponent.prototype.loadEncasResponse/<@http://192.168.88.102:4200/main.bundle.js:1131:21

I already find a way to close the modal inside the type script, but through jQuery:

jQuery('#encasUnPwModal').modal('hide');

Thank you so much @IlyaSurmay this is worked for me.

Cant make it work. i get:
ERROR TypeError: Cannot read property 'hide' of undefined when i tryr to close my modal

-> component.html file
Open model /* Open model link */

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

I found an even easier way to do this for ng bootstrap modals in cases where you call a function from inside the modal
<ng-template #content let-c="close" let-d="dismiss"> <button (click)="myFunction(c)"> </button> </ng-template>

TypeScript code for the component
myFunction(callback) { callback(); }
This solution worked for me and is very simple

step1 import angular-custom-modal
import{ModalComponent}from 'angular-custom-modal'

step 2 create Public method
export class DemoComponent implements OnInit {
@ViewChild('ModelId') public modal: ModalComponent;
}

Step 3 Use Like This
this.modal.close();

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

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;


I have find this close model and import and construction add activeModal on .ts file

Was this page helpful?
0 / 5 - 0 ratings

Related issues

webdev48 picture webdev48  路  3Comments

juanitavollmering picture juanitavollmering  路  3Comments

pgeyman picture pgeyman  路  3Comments

tuoitrexuquang picture tuoitrexuquang  路  3Comments

MrBlaise picture MrBlaise  路  3Comments