0

I am beginners for Angular2, I created a modal dialog, here is my HTML file. index.html file:-

 <button type="button" class="btn btn-default"                    (click)="modal.open()">Open</button>
    
        <modal #modal>
        <modal-header [show-close]="true">
            <h4 class="modal-title">I'm a modal!</h4>
        </modal-header>
        <modal-body>

        Here i want to call show() method from .ts file.
    </modal-body>
    <modal-footer [show-default-buttons]="true"></modal-footer>
    </modal>

After clicking on Open button I want to call show(){....} that is inside .ts file and I want to display inside <modal-boady></modal-body> tag.

Here is my hello.component.ts file:-

import {Component, View} from "angular2/core";
@Component({
  selector: 'my-app',
  templateUrl: './index.html'
  })
  
  export class  HeapMemoryGraphComponent implements OnInit {
   constructor(){}
   ngOnInit() {
    this.show();
   }
   show(){
    console.log("=====show()=======");
   }
  }

How could I do that? please guide me.

Thanks.

2
  • could you post the rest of the relevant code please? Commented Apr 17, 2017 at 14:25
  • Hey danimal, i updated my question.Could u see this once plz ? Commented Apr 17, 2017 at 14:58

2 Answers 2

1

There are several ways to get element handle in component. But most understandable guide is here.

http://valor-software.com/ngx-bootstrap/#/modals

Please check Child Modal there.

<button type="button" class="btn btn-primary" (click)="showChildModal()">Open child modal</button>
<div bsModal #childModal="bs-modal" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="mySmallModalLabel" aria-hidden="true">
  <div class="modal-dialog modal-sm">
    <div class="modal-content">
      <div class="modal-header">
        <h4 class="modal-title pull-left">Child modal</h4>
        <button type="button" class="close pull-right" aria-label="Close" (click)="hideChildModal()">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">
        I am a child modal, opened from parent component!
      </div>
    </div>
  </div>
</div>

import { Component, ViewChild } from '@angular/core';
import { ModalDirective } from 'ngx-bootstrap/modal';

@Component({
  selector: 'demo-modal-child',
  templateUrl: './child.html'
})
export class DemoModalChildComponent {
  @ViewChild('childModal') public childModal:ModalDirective;

  public showChildModal():void {
    this.childModal.show();
  }

  public hideChildModal():void {
    this.childModal.hide();
  }
}

As you can see here, component is referenced the handle of modal element in .ts file.

Sign up to request clarification or add additional context in comments.

1 Comment

It's giving error :- Template parse errors: Unhandled Promise rejection: Template parse errors: There is no directive with "exportAs" set to "bs-modal" (" <div bsModal [ERROR ->]#childModal="bs-modal" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="mySmallModalLa"):
0

html file:

<input class="search-text" type="text" (keyup)="onKeyUp($event)">
<button type="button" [disabled]="!isValidForSearch()"  class="btn btn-primary" (click)="getUsers()">Search</button>

ts file:

//Here I am getting the value entered in the text box
onKeyUp(event){
   this.searchText = event.target.value;
}

//Here I am enabling the button if user enters any text in the search text box other wise it will be disabled 
isValidForSearch() {
   var isValid = false;
   if(this.searchText != '' && this.searchText != undefined) {
       isValid = true;
   }
   return isValid;  
}

I hope this helped you.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.