3

In our project we use the linting-config from AirBnB. The is a rule, that says class methods must use this or be declared as static. In theory this rule makes a lot of sense to me, but in the angular context seems to cause some problems. Imagine a component like this (Stackblitz):

import { Component, VERSION } from '@angular/core';

@Component({
  selector: 'my-app',
  template: '<p>{{doSomething("hello stack overflow")}}'
})
export class AppComponent  {
  doSomething(string: string): string {
    return string.toLocaleUpperCase();
  }
}

Now, the linter would complain about doSomething not using this. Wen can now make the function static to satisfy it - but than we would not be able to use the function in the template.

One conclusion would be, that doSomething should not be part of AppComponent but another service for example. But than we would have to wrap the static function in non-static one again. In the end the wrapping function is not much smaller than the original one, so the whole outsourcing to service thing seems kind of pointless. Especially since we speak of functions which are explicitly only useful for the template of the component. It seems to be problematic especially with tracking function for trackBy of ngForOf - they tend to not use a this keyword by nature and are only used in template, so they can not be static.

So is there a meaningful pattern how to handle functions which are used in templates together with this rule or is it just not not a useful rule for angular?

1
  • 1
    I would say that you can use Angular pipes (angular.io/guide/pipes) for those template data transformation functions. In fact you can use them in the trackBy too (bennadel.com/blog/…). But then the linter would complain about the pipes.. So as pipes are an important part of Angular I would say that maybe the rule it´s not a good match for Angular (unless you can exclude pipes from it) Commented Dec 2, 2020 at 21:18

3 Answers 3

3

you can also define in a .ts externals functions like:

export function myFunction(name){
   return "Hello "+name;
}

You only need in one component

import {myFunction} from './myfile.ts' 

Then you can use in .ts

   myFunction("Me");

If you want to use in the html you need declare in your .ts

  myFunctionI=myFunction;

And use

  {{myFunctionI('me')}}

Other option: your .ts like

export function Util() {
    return new UtilClass()
}
class UtilClass {
   greet(name){
       return "Hello "+name;
   }
}

And you can

import {Util} from './myfile-util.ts' 

console.log(Util.greet("me"))
Sign up to request clarification or add additional context in comments.

2 Comments

But for using them in the template (the html-part of the component), I would have to wrap them in some member-functions of the component.
@Paflow, in html all the "variables" are refered to a "this". e.g. you can not use directlty in a .html {{Math.round(3.2)}}. But you can declare in your component IMath=Math and then you use {{IMath.round(3.2)}} -I like add an "I" before the "variable", but it's only a personal option-. Equal another "function" or class you want to use.
0

I found a satisfying solution myself:

I convert the those function - small, UI-related, used (only) in template, not using the scope (this) as fields, holding arrow functions.

  doSomething = (string: string): string => string.toLocaleUpperCase();

Comments

0

For your case, I think a pipe is better.

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.