In Typescript 12, the process of creating a Bootstrap popup that waits for the user to click on a value entered in

Greetings! I am currently working with Angular TypeScript 12 and I am attempting to trigger a Bootstrap modal popup when I enter a code in the input field and press enter. However, the issue is that the popup is always displayed even without typing anything.

Here is the approach I have taken:

<input (keyup.enter)="Search()" data-toggle="modal" data-target="#exampleModal" 
[formControl]="firstNameControl" type="text"  >

Here is the code for the popup modal:

<div class="modal" [class.show]="show" id="exampleModal"  role="dialog" aria-labelledby="exampleModalLabel"
    aria-hidden="true">
    <div class="modal-dialog" role="document">
    
    </div>
</div>

And here is the TypeScript code snippet:

  show: boolean = true;
  firstNameControl = new FormControl();
  code!:string;
  ngOnInit(): void {

  this.firstNameControl.valueChanges
    .pipe(debounceTime(1000))
    .subscribe(newValue => {
      this.code= newValue
    });

  showModal() {
    this.show = !this.show;
  }
 Search() {
    console.log(this.codeuser);
    this.showModal();
  }

Answer №1

The visibility of the popup is dependent on the show property, which is initially set to true:

show: boolean = true;

Consider removing the boolean value assignment in this scenario:

show: boolean;

After that, the implementation of showModal should toggle the popup on every keyup event.

For example:

export class AppComponent implements OnInit {
  @ViewChild("myModal") myModal: ElementRef;
  show = false;
  code: any;

  firstNameControl = new FormControl();

  ngOnInit() {
    this.firstNameControl.valueChanges
      .pipe(debounceTime(100))
      .subscribe((newValue) => {
        this.code = newValue;

        // If modal is shown and input being empty
        if (newValue === "") this.show = false;
      });
  }

  toggleModal() {
    this.myModal.nativeElement.click();
  }

  search() {
    if (this.code) {
      this.toggleModal();
    }
  }
}
<input (keyup.enter)="search()" [formControl]="firstNameControl" type="text" />
<button
  #myModal
  id="#modalTrigger"
  data-toggle="modal"
  data-target="#myModal"
  style="display: none"
></button>

<div id="myModal" class="modal fade show">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-body">
        <p>Some text in the modal.</p>
      </div>
    </div>
  </div>
</div>

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

The jsPDF html() method is incorrectly splitting the HTML element

I have been working on creating an invoice report using jsPDF in an Angular 7 app. The report is text-based as images are not accepted by the client. In my code, I have a main div with the id "report" which I retrieve and pass to the .html() function. With ...

Validation in Angular2 is activated once a user completes typing

My goal is to validate an email address with the server to check if it is already registered, but I only want this validation to occur on blur and not on every value change. I have the ability to add multiple controls to my form, and here is how I have st ...

Guide to adding information to a table with the help of an "interface" in Angular 6 and utilizing Typescript

Looking for some guidance with Angular as I am just starting out. I am currently trying to send an API request in angular 6 using Typescript. However, I am facing difficulties when it comes to adding data to a table. Check out my code on: Codepen In my p ...

Caution: Npm Angular alert during package installation

I keep encountering npm warnings while attempting to install packages: $ npm install npm WARN @angular/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ee8d81839e87828b9cc38d8287aedfdec0dec0dc">[email protected]</a> ...

Creating a personalized design for MUI TextField spin button

Looking to customize the appearance of the up/down spin buttons in MUI TextField. https://i.sstatic.net/DcG66.png Desiring white arrows and a black surrounding area that's slightly larger, akin to this: https://i.sstatic.net/ZxMJw.png I'm aware ...

Lazy loading in Angular 14 delays the loading of a page until the content is clicked, ensuring a smoother user experience with reduced loading times

Greetings, I've been stuck on this issue for quite some time now. The problem I'm facing is that the page doesn't load until I click on the website. Can anyone please assist me with this? My expectation is that the default setting I have co ...

Upon running `npm run build` in vue.js, an error occurs stating that the interface 'NodeRequire' cannot extend types 'Require' simultaneously

ERROR in C:/phpStudy2018/PHPTutorial/WWW/Tms.Web/node_modules/@types/node/globals.d.ts(139,11): 139:11 The 'NodeRequire' interface cannot extend both 'Require' and 'RequireFunction' at the same time. The named property &apos ...

Encountered a module build error while upgrading Angular Project from version 14 to 15

When attempting to run my project, an error is displayed. ./src/styles.scss?ngGlobalStyle - Error: Module build failed (from ./node_modules/mini-css-extract-plugin/dist/loader.js): HookWebpackError: Module build failed (from ./node_modules/sass-loader/dist ...

What is the best way to extract data from API console output using Angular?

Currently, I am deeply involved in a full stack project utilizing asp.net and angularjs. My goal is to extract output response from the Swagger API. You can view the code I've written for this purpose here: (https://i.stack.imgur.com/vLyIE.png) The A ...

Exploring Angular: How does Number.isNaN handle non-empty strings?

Within my component, there is a dropdown menu that allows users to choose a value. Upon selecting an item from the dropdown, a function called onDropdownChange is triggered. The parameter passed to this function can either be a string ("Please select an op ...

A guide on implementing JSON data projection with TypeScript

{ "ClaimType": ["The 'Claim Type' should have a minimum length of 4 characters. You have only entered 2 characters."], "ClaimValue": ["The 'Claim Value' should have a minimum length of 4 characters. You have only entered 1 chara ...

Troubleshooting: NextJS Typescript getInitialProps returning null value

I am currently working with NextJS 'latest' and TypeScript to extract the token from the URL, but I am encountering an issue where it returns undefined. To achieve this, I am utilizing the getInitialProps method. The URL in question looks like th ...

The field 'XXX' is not a valid property on the type 'CombinedVueInstance<Vue, {}, {}, {}, Readonly<Record<never, any>>>'

When creating a Vue component with TypeScript, I encountered an error message in the data() and methods() sections: Property 'xxx' does not exist on type 'CombinedVueInstance<Vue, {}, {}, {}, Readonly<Record<never, any>>>& ...

Deploying a single Angular 5 project to various clients with unique URLs can streamline the development process

I am currently working on a new project using Angular 5.2 version. It has been deployed to the DEV location with the following URL: http://XX.XX.XX.XX/myDev/app/login When I execute the ng build command: ng build -aot -ec --prod --base-href=/myDev/ Now ...

Establish a connection between a React variable and state management

In my codebase, I have an external module file named Task.ts. It contains the following: const taskList: Task[] = []; Class Task { ... } export { Task, taskList } The taskList is a list of Task objects that can be modified by the Task class. Now, i ...

Use Angularfire 5 to merge data from multiple lists based on a common key

Ever since the release of Angularfire5, using $key has become a thing of the past. I decided to convert this and stumbled upon a snippet that shows how to change the list into one containing their own key: getRankings(week): Observable<any> { ...

Nestjs struggles with resolving dependencies

Having trouble finding the issue in my code. (I'm still new to nestjs and trying to learn by working on some apps). The console log is showing the following error: Nest can't resolve dependencies of the UrlsAfipService (?). Please ensure tha ...

How do you transfer byte[] data using a DTO in Java Spring?

I am currently facing an issue with my server-side application. The problem arises when attempting to convert a Blob to an Excel file on the front-end, specifically when the byte[] is sent within a DTO. When sending a POST request from the back-end (sprin ...

Angular 5 using AngularFire for Seamless Firebase Login Experience

Utilizing AngularFire2 in combination with Firebase and Angular 5, I have successfully implemented a login system. However, upon refreshing the page, a slight flicker occurs due to the delay in loading JavaScript. This issue is unfamiliar to me as I'm ...

Guide on bringing in Typescript definition that exports a lone variable

The "@types/dd-trace" library defines a single variable called trace in the type definition for the "dd-trace" library. declare var trace: TraceProxy; export = trace; declare class TraceProxy extends Tracer { /** * Initializes the tracer. This s ...