Utilizing the Click event in TypeScript

I'm having trouble getting my onclick event to work in an Angular 2 application. Here is a snippet of my code:

HTML- (home.component.html): Code Snippet

 <!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=Edge">
    <title>Iroz-V3 - Sutherland Global Services</title>
    <link href="../../Content/style.css" rel="stylesheet" />
    <script src="../../Scripts/jquery.js"></script>
</head>

<body class="login_bg">
    <div id="wrapper">
        <div id="mask">
            <div id="item1" class="item">
                <div class="program-wrap">
                    <div class="log_wrap">
                        <div class="log_img"></div>
                        <ul class="log_list">
                            <li>
                                USER ID<br>
                                <input class="log_input" id="NtLogin" placeholder="NT Login ID" type="text">
                            </li>
                            <li>
                                PASSWORD<br>
                                <input class="log_input" placeholder="*******" type="password">
                            </li>
                            <li>
                                <input type="button" class="reset_but" value="Reset" />
                                <a class="india panel">
                                    <input type="button" class="log_but" value="Login" (Click)="login_btnClick()" />
                                </a>
                            </li>
                        </ul>
                    </div>
                </div>
            </div>
        </div>
    </div>
</body>
</html>

TS-(home.component.ts): TypeScript File

    import { Component, OnInit} from "@angular/core";
@Component({
    template: `app/Components/home.component.html`
})

export class HomeComponent implements OnInit{
    ngOnInit() { }
    public login_btnClick() {
        console.log("function called");
        alert("login");
    }
}

The function is not being called and no errors are showing in the console.

Answer №1

Make sure to wrap the function call in double quotation marks within your HTML code.

<button class="log_but" value="Login" id="login" (click)="login()">Login</button>

In addition, remember to update your home.component.ts file to use templateUrl instead of template. Don't forget to include a constructor for your component as well.

@Component({
    selector: 'app-root',
    templateUrl: 'home.component.html'
})

export class HomeComponent implements OnInit { 
    constructor() { }
    ngOnInit() { }
    login() {
        alert('login clicked');
    }
}

UPDATE

As mentioned in the comments, change (Click) to lowercase (click).

Answer №2

import { Component, OnInit} from "@angular/core";

@Component({
  selector: 'app-root',
  template: `<button (click)="login()">Click</button>`
})

export class HomeComponent implements OnInit{
    ngOnInit() { }
    login() {
        alert("login");
    }
}

your current syntax is as follows:

template: app/Components/home.component.html

Please update it to the following:

templateUrl: './home.component.html'

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

Attempting to deploy Angular 9 application to Heroku was successful, however, encountered issues preventing the application from

Response: I encountered an issue where the packages "express" and "body-parser" were missing from my package.json dependencies. "express":"^4.17.1", "body-parser":"^1.19.0" This caused complications when attempting to deploy my Angular 9 app on Heroku. ...

Angular: The most efficient method for creating a customized HTML page with unique styles specifically for PDF generation

Struggling with PDF generation and the challenge of preparing hidden HTML is my current hurdle. The backend team has built a PDF generation service that requires passing an HTML string as a parameter. My task is to hide all HTML elements upon clicking the ...

Unable to load component "/" in Angular 2 Rc.1 router

I've been trying to implement the new router from the rc.0 update (actually using rc.1), but I'm running into an issue with the outlet not loading the "Welcome" component. Below is the code in app.component.ts import { Component } from '@a ...

Displaying md-error within md-input-container using Angular without relying on Validators within the associated formControl

I am working on a form element with an input field <md-input-container class="margin-top-little"> <input mdInput placeholder="{{placeholder}}" [(ngModel)]="value" (blur)="onBlur()" name="{{name}}" [formControl]="validator" (change)="onCha ...

Is there a way to cancel or undo a transaction in the middle of using the PayPal JavaScript SDK?

As a newcomer to Angular, I am working on integrating PayPal as a payment gateway. However, I am unsure of the correct procedure to follow. paypal .Buttons({ createOrder: (data, actions) => { return actions.order.create({ purchase_ ...

Invoke an RxJs observable to handle errors and retry the process

I have an observable called submit$ that submits a form. If this observable encounters an error with status code 403, it means the user is not authorized and needs to log in first. Is there a way to automatically trigger another observable when a specific ...

Tips for updating property values when calling a TypeScript function

Hello everyone, I am looking to convert a snippet of JavaScript code into TypeScript. JavaScript function newState(name){ var state ={ name : name, age : 0 } return state } function initStates() { this.JamesStat ...

The communication between the frontend and backend is obstructed

For my fullstack application, I am using the koa framework in node.js for the backend and Angular 4 for the frontend. The issue I'm facing is when I try to make a post request to the backend API from the frontend (specifically during sign-in), I recei ...

What is the alternative to using [(ngModel)] to align values between the model and view in Angular?

I am working with a list of ranges and need to validate them before updating the model. Specifically, each range should not be lower than the previous one or higher than the next. <tr *ngFor="let row of ranges> <td> <input ...

Bring in all iterable arrays from TypeScript using the import statement

How can TypeScript be used to access multiple classes from different files as an array? Consider the following folder structure: ├── index.ts └── models ├── index.ts ├── image.entity.ts └── user.entity.ts In the ...

Executing Promises in a loop: TypeScript & Angular with IndexedDB

Currently, I am working on a data synchronization service where data is being retrieved from a web service and then stored in IndexedDB. In my TypeScript Angular Service, the code looks something like this: this.http .post(postUrl, postData) .suc ...

What is the role of authguard in securing routes?

When developing an application, I encountered the need to implement authorization to protect routes using AuthGuard. However, I now face the challenge of securing child routes based on a role system obtained from the backend during login. For example, if t ...

Is it possible for an app's feature module to access routing configurations from another lazily loaded module in Angular routing?

The functionality of our App is divided into multiple feature modules that are lazily loaded. Each module is loaded under different path matches, and some modules import a shared module containing reusable components. Everything seems to be working well so ...

What is the best practice for updating the state of a web component in a manner that can be tracked by history with vaadin-router?

I have a straightforward LitElement custom component that I would like to incorporate an "editing" state into. Although I already possess all the necessary information from the server, I am hesitant to introduce a new component solely for displaying an edi ...

Transmitting MQTT information through an application programming interface

In my project using Ionic React, I am developing an application to showcase temperature data. To achieve this, I have established an API that transmits MQTT temperature information and utilize Axios for data retrieval. Despite my efforts, I am encountering ...

Angular: understanding the intricacies of initializing variables within the *ngFor directive

I'm working with an Angular template that looks like this: <li *ngFor="let item of list | keyvalue" let-rand="random()"> <input type="radio" class="form-check-input" id="radio-{{rand}}" name ...

Can you explain the functionality of this Angular CLI instruction?

Can you explain the function of this command in the Angular command line? npx ng g s weather --flat false Referenced in: Angular 6 for Enterprise Ready Web Applications, page 61 ...

Various modules in the project need to have distinct GitHub origins, particularly in the case of Spring-Angular

My goal is to create a well-structured project with separate frontend and backend modules. Here is the initial project structure: https://i.stack.imgur.com/EghPA.png I have attempted this in various configurations before, but every time I try, git recogn ...

Mistakes following the modification of tsconfig.json and package.json

Recently, I delved into exploring the Ahead-of-time compilation cookbook for Angular and found it quite intriguing. However, it seems like the errors I am encountering are not directly related to my new venture. You can check out the cookbook here: https:/ ...

Using Angular 5 to link date input to form field (reactive approach)

I'm encountering an issue with the input type date. I am trying to bind data from a component. Below is my field: <div class="col-md-6"> <label for="dateOfReport">Data zgłoszenia błędu:</label> <input type="date" formC ...