MMR
MMR

Reputation: 3009

How to call a function in typescript

I am trying to call function through my template click event but the error is "get is not a function". Can someone find where the issue is?

My template:

    <button class="btn btn-primary" (click)="get()">sign-up</button>

My ts:

import {Component} from '@angular/core';
import {FORM_DIRECTIVES, REACTIVE_FORM_DIRECTIVES} from '@angular/forms';
import {FormBuilder, FormGroup, Validators} from '@angular/forms'
import { Http, Headers } from '@angular/http';
import { Router, ROUTER_DIRECTIVES } from '@angular/router';
@Component({
  directives: [FORM_DIRECTIVES, REACTIVE_FORM_DIRECTIVES],
     templateUrl : "./components/login/login.html",
   })
      export class Login {
   //http: Http;           
     form: FormGroup;    

   constructor(public router: Router,  public http: Http, fbld:    FormBuilder) {
   // this.http = http;
    this.form = fbld.group({
        email: ['', Validators.required],
        phone: ['', Validators.required]
     });

  }
   function get(){
  //  this.router.navigate(['/demo/signup']);
    alert('hai');
  }

Upvotes: 0

Views: 142

Answers (2)

In typescript the method is declared like this:

public get() {
   ....
}

Your function don't work, because a declaration like that is an internal function for javascript objects. To use like function do something like this:

public get = function() {
    ....
}

Upvotes: 1

micronyks
micronyks

Reputation: 55443

As long as you are playing with Typescript, it is not required to define a function using function keyword.

So change it to,

get(){
   ...
}

Upvotes: 1

Related Questions