1

I am trying to call javascript function from the typescript function. This is not working at all.

I have created sudo code in stackblitz. Please check the same.

https://stackblitz.com/edit/angular-nullyo

3
  • Maybe you should clarify that you're trying to import the hello() for an external script "src/external/sample.js"... Commented Apr 13, 2019 at 18:42
  • 2
    This may be an issue with Stackblitz (the scripts in the angular.json specifically) as your example works in a local new project. If you put a console.log(...) statement in the sample.js file outside of that function to ensure it gets loaded, it never gets called in Stackblitz. Commented Apr 13, 2019 at 18:59
  • @DanielWStrimpel I think that too. But as per stavam 's answer if we export it that works well. I am not sure how export actually works with stackblitz or this is the alwasys case we should mention export for each function in javascript.? Commented Apr 15, 2019 at 5:31

1 Answer 1

2

You can export it at one place, import it at another.

hello.js

export function hello() {
  console.log('hello world');
}

app.component.ts

import { hello } from '../external/hello.js';

@Component({
  // ...
})
export class AppComponent  {   
  buttonClick() {
    console.log('btn click called. 1.');
    hello();
    console.log('btn click called. 2.');
  }
}

DEMO

Sign up to request clarification or add additional context in comments.

2 Comments

Why it is working locally but not in stackblitz? I mean it is every time need to import and not just declaring it in package.jso
Thanks for the quick answer. I have one sequential problem for which I have actually created stackblitz sudo and came across this problem. Please check this if it is the proper way to perform scenario [link][1]. [1]: stackoverflow.com/questions/55683030/…

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.