Creating and calling a custom function inside an angular class

omar

I created a custom function inside a class, how do I call it within the same class?

A user enters a word in an input field. It triggers the "onNameKeyUp" function below. I have to verify if the word is plural: if yes I turn it into a singular form. I am giving below a very simplified example:

  export class HomeComponent implements OnInit {
  spelling="";

  tokenizeSpellingEnglish(item: string): string{
  if (item =="houses"){
    item = "house";
  }
     return item;
  }

   onNameKeyUp(event: any){
    this.spelling = event;
    this.spelling = this.spelling.toLowerCase();
    this.tokenizeSpellingEnglish(this.spelling);
    this.elem.nativeElement.focus();   
   }

The singular form will then be sent to the server for further processing.

the application works fine without the spelling verification.

As a side note, I am wondering if this is the right approach at all: should I parse the user input on the server instead? That being said, still I would love to know what is wrong with the above code.

night_owl

If I understand you correctly, and your example code is truly representative of your real code, I see that you are calling tokenizeSpellingEnglish but never storing the returned value back into this.spelling:

export class HomeComponent implements OnInit {
  spelling="";

  tokenizeSpellingEnglish(item: string): string{
  if (item =="houses"){
    item = "house";
  }
     return item;
  }

   onNameKeyUp(event: any){
    this.spelling = event;
    this.spelling = this.spelling.toLowerCase();

    // --> this.tokenizeSpellingEnglish(this.spelling); should be
    this.spelling = this.tokenizeSpellingEnglish(this.spelling);

    this.elem.nativeElement.focus();   
   }

You could also have tokenizeSpellingEnglish work directly on this.spelling, if that suits your use case.

If your code is instead simply failing to execute properly, I'd look at how you are unpacking the event parameter in onNameKeyUp. If you are handling the raw event object from the 'keyup' event, you need to get the value from the target property on the event object and work with that:

onNameKeyUp(event: any) {
  this.spelling = event.target.value;
  // the rest of your function as before
}

HTH

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Angular 2 custom validation not calling inside of component function

From Dev

Calling a recursive function inside a class

From Dev

Initiating and calling a function inside class

From Dev

Calling an async function inside a class

From Dev

Creating a function inside a function inside a class

From Dev

calling function inside angular factory

From Dev

PHP Calling A Class function inside B Class

From Dev

Creating a UIImageView inside a custom UITableViewCell Class (Swift)

From Dev

JS - Calling class methods inside of callback function

From Java

Calling a class function inside of __init__

From Dev

Calling a function inside same class not working, typescript

From Dev

Calling a function pointer inside the same class

From Dev

Creating a Global Instance of a Class Inside of a Function

From Dev

Creating a DataBase Table inside a Class as a Function

From Dev

Creating Unittest class inside function not working

From Dev

Calling function inside callback throws error in Angular

From Dev

calling function inside javascript class : this.function is not a function

From Dev

Creating and calling custom exceptions

From Dev

Angular - Calling a function from an extended class

From Dev

Call a function inside a class when calling the class in python

From Dev

calling a function inside a class from another class php

From Dev

C++ calling a class function inside another function

From Java

Defining custom function/property inside anonymous class

From Dev

What am i doing wrong in creating and calling this simple class function?

From Dev

Angular - calling service function inside eventlistener function returns undefined error

From Dev

angular 2 function calling inside an http post function

From Dev

Angular 5:Calling service method inside custom validator

From Dev

Calling a custom scalar DB function inside a LINQ to Entities query?

From Dev

Android: Calling a function inside a fragment from a custom action bar

Related Related

  1. 1

    Angular 2 custom validation not calling inside of component function

  2. 2

    Calling a recursive function inside a class

  3. 3

    Initiating and calling a function inside class

  4. 4

    Calling an async function inside a class

  5. 5

    Creating a function inside a function inside a class

  6. 6

    calling function inside angular factory

  7. 7

    PHP Calling A Class function inside B Class

  8. 8

    Creating a UIImageView inside a custom UITableViewCell Class (Swift)

  9. 9

    JS - Calling class methods inside of callback function

  10. 10

    Calling a class function inside of __init__

  11. 11

    Calling a function inside same class not working, typescript

  12. 12

    Calling a function pointer inside the same class

  13. 13

    Creating a Global Instance of a Class Inside of a Function

  14. 14

    Creating a DataBase Table inside a Class as a Function

  15. 15

    Creating Unittest class inside function not working

  16. 16

    Calling function inside callback throws error in Angular

  17. 17

    calling function inside javascript class : this.function is not a function

  18. 18

    Creating and calling custom exceptions

  19. 19

    Angular - Calling a function from an extended class

  20. 20

    Call a function inside a class when calling the class in python

  21. 21

    calling a function inside a class from another class php

  22. 22

    C++ calling a class function inside another function

  23. 23

    Defining custom function/property inside anonymous class

  24. 24

    What am i doing wrong in creating and calling this simple class function?

  25. 25

    Angular - calling service function inside eventlistener function returns undefined error

  26. 26

    angular 2 function calling inside an http post function

  27. 27

    Angular 5:Calling service method inside custom validator

  28. 28

    Calling a custom scalar DB function inside a LINQ to Entities query?

  29. 29

    Android: Calling a function inside a fragment from a custom action bar

HotTag

Archive