Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Importing generic JS functions into Angular module

Bit of an Angular/Typescript newb here.

I've set up a JavaScript library of exported, helper functions that I know want to import into an Angular components, and use in those components' .html template files.

Here's my Javascript library, template-functions/data-types.ts:

export const isNumeric = val => !Array.isArray(val) && (val - parseFloat(val) + 1) >= 0;

It's a simple arrow function that I'm exporting:

Here's how I'm importing it in my component:

import { Component, OnInit, Input } from '@angular/core';
import { MyModel } from '../model/my-model';
import { isNumeric } from '../template-functions/data-types';

@Component({
  selector: 'my-module',
  templateUrl: './my-module.html'
})

export class MyModule implements OnInit {

  @Input() public mymodeldetails: MyModel;

  constructor() { }

  ngOnInit() { }

  // isNumeric = val => !Array.isArray(val) && (val - parseFloat(val) + 1) >= 0;
}

This is after several hours of having the Angular compiler and VSCode telling me that it couldn't find the data-types module, even though the path was correct. I had a .js file extensions instead of a .ts one!

So now Angular can see my isNumeric function, but I'm the .html template file does not see it with what I've done so far. The browser throws the error "_co.isNumeric is not a function".

What more do I need to do to get my Angular template to see my imported function?

I'm using Angular 6.

like image 258
ChillyPenguin Avatar asked Oct 09 '18 02:10

ChillyPenguin


People also ask

How do I import a function in TypeScript?

To import a function from another file in TypeScript: Export the function from file A , e.g. export function sum() {} . Import the function in file B as import { sum } from './another-file' . Use the imported function in file B .

What is @NgModule in Angular?

@NgModule takes a metadata object that describes how to compile a component's template and how to create an injector at runtime. It identifies the module's own components, directives, and pipes, making some of them public, through the exports property, so that external components can use them.

Can we export function in Angular?

By using the export keyword we say which of those functions can be exported, and therefore imported and used in other modules.

Can you import a function in Javascript?

You can import functions from a different file using the import keyword functionality. Import allows you to choose which part of a file or module to load. This will make this function available for use in our current file.


1 Answers

To make it visible in the template, wrap it in a component method:

import { isNumeric } from '../template-functions/data-types';

export class MyComponent implements OnInit {

  isNumeric(val: any): boolean {
    return isNumeric(val);
  }
}

See this stackblitz for a demo.


As explained in the Angular template syntax documentation:

The expression context is typically the component instance. (...) Template expressions cannot refer to anything in the global namespace (except undefined). They can't refer to window or document. They can't call console.log or Math.max. They are restricted to referencing members of the expression context.

like image 120
ConnorsFan Avatar answered Sep 25 '22 21:09

ConnorsFan