Unable to call javascript function from Typescript in Angular 6 The 2019 Stack Overflow Developer Survey Results Are In Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) The Ask Question Wizard is Live! Data science time! April 2019 and salary with experienceAngular 6 - assets not includingAngular 2 App Component constructor and ngoninit not calledGet incorrect offsetWidth and offsetHeight valuesHow to get offsetWidth and offsetHeight values after add css class to change themWhy ngOnInit is getting called every time repeatedlyMapplic with Angular 4How in angular 2+, dynamically manage the addition of animations (:enter and :leave) under certain conditions?Using materialize-css (v 1.0.0) in Angular 5 does not workAngular http request$Injector Error on Angular Upgrade from 1.6.6 to 6angular :how to rotate an image with animation from component?
What's the point in a preamp?
Didn't get enough time to take a Coding Test - what to do now?
Take groceries in checked luggage
What can I do if neighbor is blocking my solar panels intentionally?
He got a vote 80% that of Emmanuel Macron’s
system() function string length limit
Is it ok to offer lower paid work as a trial period before negotiating for a full-time job?
Would an alien lifeform be able to achieve space travel if lacking in vision?
How can I define good in a religion that claims no moral authority?
First use of “packing” as in carrying a gun
Can undead you have reanimated wait inside a portable hole?
Wall plug outlet change
Can a 1st-level character have an ability score above 18?
Why can't devices on different VLANs, but on the same subnet, communicate?
How do I add random spotting to the same face in cycles?
Derivation tree not rendering
Single author papers against my advisor's will?
Sort a list of pairs representing an acyclic, partial automorphism
Can the prologue be the backstory of your main character?
How should I replace vector<uint8_t>::const_iterator in an API?
What are these Gizmos at Izaña Atmospheric Research Center in Spain?
Mortgage adviser recommends a longer term than necessary combined with overpayments
Make it rain characters
Was credit for the black hole image misattributed?
Unable to call javascript function from Typescript in Angular 6
The 2019 Stack Overflow Developer Survey Results Are In
Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)
The Ask Question Wizard is Live!
Data science time! April 2019 and salary with experienceAngular 6 - assets not includingAngular 2 App Component constructor and ngoninit not calledGet incorrect offsetWidth and offsetHeight valuesHow to get offsetWidth and offsetHeight values after add css class to change themWhy ngOnInit is getting called every time repeatedlyMapplic with Angular 4How in angular 2+, dynamically manage the addition of animations (:enter and :leave) under certain conditions?Using materialize-css (v 1.0.0) in Angular 5 does not workAngular http request$Injector Error on Angular Upgrade from 1.6.6 to 6angular :how to rotate an image with animation from component?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
The javascript function (help) below is not being called from ngOnInit()?
In web console: ERROR ReferenceError: "help is not defined"
Would you know how this can be done?
src/assets/myjs.js:
function help()
console.log("hilfe");
src/app/highlight.directive.ts:
import Directive, ElementRef, OnInit from '@angular/core';
declare function help() : any;
@Directive(
selector: '[appHighlight]'
)
export class HighlightDirective implements OnInit
constructor(el: ElementRef)
el.nativeElement.style.backgroundColor = 'yellow';
ngOnInit()
help();
angular.json:
...
"styles": [
"src/styles.css"
],
"scripts": [
"src/assets/myjs.js"
],
},
...
angular
add a comment |
The javascript function (help) below is not being called from ngOnInit()?
In web console: ERROR ReferenceError: "help is not defined"
Would you know how this can be done?
src/assets/myjs.js:
function help()
console.log("hilfe");
src/app/highlight.directive.ts:
import Directive, ElementRef, OnInit from '@angular/core';
declare function help() : any;
@Directive(
selector: '[appHighlight]'
)
export class HighlightDirective implements OnInit
constructor(el: ElementRef)
el.nativeElement.style.backgroundColor = 'yellow';
ngOnInit()
help();
angular.json:
...
"styles": [
"src/styles.css"
],
"scripts": [
"src/assets/myjs.js"
],
},
...
angular
1
You need to declare that this function exists in the global scope: typescriptlang.org/docs/handbook/declaration-files/…, github.com/angular/angular-cli/wiki/…. But the main question is: why do you have that JS script and this global function in the first place?
– JB Nizet
Aug 14 '18 at 6:34
add a comment |
The javascript function (help) below is not being called from ngOnInit()?
In web console: ERROR ReferenceError: "help is not defined"
Would you know how this can be done?
src/assets/myjs.js:
function help()
console.log("hilfe");
src/app/highlight.directive.ts:
import Directive, ElementRef, OnInit from '@angular/core';
declare function help() : any;
@Directive(
selector: '[appHighlight]'
)
export class HighlightDirective implements OnInit
constructor(el: ElementRef)
el.nativeElement.style.backgroundColor = 'yellow';
ngOnInit()
help();
angular.json:
...
"styles": [
"src/styles.css"
],
"scripts": [
"src/assets/myjs.js"
],
},
...
angular
The javascript function (help) below is not being called from ngOnInit()?
In web console: ERROR ReferenceError: "help is not defined"
Would you know how this can be done?
src/assets/myjs.js:
function help()
console.log("hilfe");
src/app/highlight.directive.ts:
import Directive, ElementRef, OnInit from '@angular/core';
declare function help() : any;
@Directive(
selector: '[appHighlight]'
)
export class HighlightDirective implements OnInit
constructor(el: ElementRef)
el.nativeElement.style.backgroundColor = 'yellow';
ngOnInit()
help();
angular.json:
...
"styles": [
"src/styles.css"
],
"scripts": [
"src/assets/myjs.js"
],
},
...
angular
angular
asked Aug 14 '18 at 6:28
Helen ReevesHelen Reeves
1561021
1561021
1
You need to declare that this function exists in the global scope: typescriptlang.org/docs/handbook/declaration-files/…, github.com/angular/angular-cli/wiki/…. But the main question is: why do you have that JS script and this global function in the first place?
– JB Nizet
Aug 14 '18 at 6:34
add a comment |
1
You need to declare that this function exists in the global scope: typescriptlang.org/docs/handbook/declaration-files/…, github.com/angular/angular-cli/wiki/…. But the main question is: why do you have that JS script and this global function in the first place?
– JB Nizet
Aug 14 '18 at 6:34
1
1
You need to declare that this function exists in the global scope: typescriptlang.org/docs/handbook/declaration-files/…, github.com/angular/angular-cli/wiki/…. But the main question is: why do you have that JS script and this global function in the first place?
– JB Nizet
Aug 14 '18 at 6:34
You need to declare that this function exists in the global scope: typescriptlang.org/docs/handbook/declaration-files/…, github.com/angular/angular-cli/wiki/…. But the main question is: why do you have that JS script and this global function in the first place?
– JB Nizet
Aug 14 '18 at 6:34
add a comment |
4 Answers
4
active
oldest
votes
What about exporting your function like
module.exports.help = () =>
console.log("hilfe");
Then
const helper = require("./pathofyourfile");
add a comment |
@simon-reeves
"scripts": [
"src/assets/myjs.js"
],
I had to put testjs.js
file into assets
folder and applied path like below
moreover, make sure that assets
section is defined an above and that file is exists by this path in sources
In addition, check content of the generated scripts.js
file , cuz, it should contains your help
function, e.g. in my case it's myalert
function
hope it should help you
add a comment |
There are two sections in the angular.json file for scripts. One is "architect:" the other is "test:". Is your scripts definition in both places?
"scripts": [ "src/assets/myjs.js" ],
And I doubt this second point would make a difference, but I was guided by one post to create a sub-directory under assets called "js". Like this...
"scripts": [ "src/assets/js/myjs.js" ],
You also might try an alert('hilfe') in your js function rather than a console.log()... although either should work.
Your setup seems pretty correct otherwise.
In additional to that you need to declare the same function in your component file. For example the function name is JavaScript file is help(), you need to declare it just above the component like:
declare function help(): any;
and then call the help function where you want to call in that file.
add a comment |
ES6/TS
Simply export your function Help in help.ts
export default function Help()
Import anywhere by
import Help from 'help'
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f51834966%2funable-to-call-javascript-function-from-typescript-in-angular-6%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
What about exporting your function like
module.exports.help = () =>
console.log("hilfe");
Then
const helper = require("./pathofyourfile");
add a comment |
What about exporting your function like
module.exports.help = () =>
console.log("hilfe");
Then
const helper = require("./pathofyourfile");
add a comment |
What about exporting your function like
module.exports.help = () =>
console.log("hilfe");
Then
const helper = require("./pathofyourfile");
What about exporting your function like
module.exports.help = () =>
console.log("hilfe");
Then
const helper = require("./pathofyourfile");
answered Aug 14 '18 at 6:32
erdemgunencerdemgunenc
166514
166514
add a comment |
add a comment |
@simon-reeves
"scripts": [
"src/assets/myjs.js"
],
I had to put testjs.js
file into assets
folder and applied path like below
moreover, make sure that assets
section is defined an above and that file is exists by this path in sources
In addition, check content of the generated scripts.js
file , cuz, it should contains your help
function, e.g. in my case it's myalert
function
hope it should help you
add a comment |
@simon-reeves
"scripts": [
"src/assets/myjs.js"
],
I had to put testjs.js
file into assets
folder and applied path like below
moreover, make sure that assets
section is defined an above and that file is exists by this path in sources
In addition, check content of the generated scripts.js
file , cuz, it should contains your help
function, e.g. in my case it's myalert
function
hope it should help you
add a comment |
@simon-reeves
"scripts": [
"src/assets/myjs.js"
],
I had to put testjs.js
file into assets
folder and applied path like below
moreover, make sure that assets
section is defined an above and that file is exists by this path in sources
In addition, check content of the generated scripts.js
file , cuz, it should contains your help
function, e.g. in my case it's myalert
function
hope it should help you
@simon-reeves
"scripts": [
"src/assets/myjs.js"
],
I had to put testjs.js
file into assets
folder and applied path like below
moreover, make sure that assets
section is defined an above and that file is exists by this path in sources
In addition, check content of the generated scripts.js
file , cuz, it should contains your help
function, e.g. in my case it's myalert
function
hope it should help you
answered Aug 14 '18 at 9:02
Damir BeylkhanovDamir Beylkhanov
19211
19211
add a comment |
add a comment |
There are two sections in the angular.json file for scripts. One is "architect:" the other is "test:". Is your scripts definition in both places?
"scripts": [ "src/assets/myjs.js" ],
And I doubt this second point would make a difference, but I was guided by one post to create a sub-directory under assets called "js". Like this...
"scripts": [ "src/assets/js/myjs.js" ],
You also might try an alert('hilfe') in your js function rather than a console.log()... although either should work.
Your setup seems pretty correct otherwise.
In additional to that you need to declare the same function in your component file. For example the function name is JavaScript file is help(), you need to declare it just above the component like:
declare function help(): any;
and then call the help function where you want to call in that file.
add a comment |
There are two sections in the angular.json file for scripts. One is "architect:" the other is "test:". Is your scripts definition in both places?
"scripts": [ "src/assets/myjs.js" ],
And I doubt this second point would make a difference, but I was guided by one post to create a sub-directory under assets called "js". Like this...
"scripts": [ "src/assets/js/myjs.js" ],
You also might try an alert('hilfe') in your js function rather than a console.log()... although either should work.
Your setup seems pretty correct otherwise.
In additional to that you need to declare the same function in your component file. For example the function name is JavaScript file is help(), you need to declare it just above the component like:
declare function help(): any;
and then call the help function where you want to call in that file.
add a comment |
There are two sections in the angular.json file for scripts. One is "architect:" the other is "test:". Is your scripts definition in both places?
"scripts": [ "src/assets/myjs.js" ],
And I doubt this second point would make a difference, but I was guided by one post to create a sub-directory under assets called "js". Like this...
"scripts": [ "src/assets/js/myjs.js" ],
You also might try an alert('hilfe') in your js function rather than a console.log()... although either should work.
Your setup seems pretty correct otherwise.
In additional to that you need to declare the same function in your component file. For example the function name is JavaScript file is help(), you need to declare it just above the component like:
declare function help(): any;
and then call the help function where you want to call in that file.
There are two sections in the angular.json file for scripts. One is "architect:" the other is "test:". Is your scripts definition in both places?
"scripts": [ "src/assets/myjs.js" ],
And I doubt this second point would make a difference, but I was guided by one post to create a sub-directory under assets called "js". Like this...
"scripts": [ "src/assets/js/myjs.js" ],
You also might try an alert('hilfe') in your js function rather than a console.log()... although either should work.
Your setup seems pretty correct otherwise.
In additional to that you need to declare the same function in your component file. For example the function name is JavaScript file is help(), you need to declare it just above the component like:
declare function help(): any;
and then call the help function where you want to call in that file.
edited Mar 22 at 6:33
Community♦
11
11
answered Sep 17 '18 at 22:35
Harry WhitehouseHarry Whitehouse
155
155
add a comment |
add a comment |
ES6/TS
Simply export your function Help in help.ts
export default function Help()
Import anywhere by
import Help from 'help'
add a comment |
ES6/TS
Simply export your function Help in help.ts
export default function Help()
Import anywhere by
import Help from 'help'
add a comment |
ES6/TS
Simply export your function Help in help.ts
export default function Help()
Import anywhere by
import Help from 'help'
ES6/TS
Simply export your function Help in help.ts
export default function Help()
Import anywhere by
import Help from 'help'
answered Mar 22 at 6:43
BoobalanBoobalan
239110
239110
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f51834966%2funable-to-call-javascript-function-from-typescript-in-angular-6%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
You need to declare that this function exists in the global scope: typescriptlang.org/docs/handbook/declaration-files/…, github.com/angular/angular-cli/wiki/…. But the main question is: why do you have that JS script and this global function in the first place?
– JB Nizet
Aug 14 '18 at 6:34