0

How to call function expression of JavaScript from angular 7?

My problem is similar to this

here is my test.js:

'use strict';
var val = (function() {
    function launch() {
        console.log("val");
    };
    return {
        launch: launch
    };
})();

I wanna call launch method from Angular Typescript I am trying as:

declare var val2: any;

ngOnInit() {
    val.launch();
}
veben
  • 19,637
  • 14
  • 60
  • 80
  • 1
    What is the problem? – SLaks Dec 27 '18 at 14:50
  • Possible duplicate of [angular 2 how to import custom function to component and service](https://stackoverflow.com/questions/41438801/angular-2-how-to-import-custom-function-to-component-and-service) – FK82 Dec 27 '18 at 14:58
  • Possible duplicate of [Angular2: import external js file into component](https://stackoverflow.com/questions/37081943/angular2-import-external-js-file-into-component) – Patrick Dec 27 '18 at 15:01

1 Answers1

1

It's quite hard to understand your question, but I'll have a shot:

The source code in Angular projects is composed of ES modules, ie functions and variables are passed from one file to another via imports and exports.

If you want to use val from test.js in some.component.ts, you might want to do that:

In test.js:

var val = /* ... */
export { val }

In some.component.ts:

import { val } from './test.js';

@Component({...})
export class SomeComponent {
  ngOnInit() {
    val.launch();
  }
}
Nino Filiu
  • 16,660
  • 11
  • 54
  • 84