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;








1















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"
],
},
...









share|improve this question

















  • 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















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"
],
},
...









share|improve this question

















  • 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








1








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"
],
},
...









share|improve this question














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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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













  • 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













4 Answers
4






active

oldest

votes


















2














What about exporting your function like



module.exports.help = () => 
console.log("hilfe");



Then



const helper = require("./pathofyourfile");





share|improve this answer






























    1














    @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



    enter image description here



    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



    enter image description here



    hope it should help you






    share|improve this answer






























      1














      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.






      share|improve this answer
































        0














        ES6/TS



        Simply export your function Help in help.ts



        export default function Help()


        Import anywhere by



        import Help from 'help'





        share|improve this answer























          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
          );



          );













          draft saved

          draft discarded


















          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









          2














          What about exporting your function like



          module.exports.help = () => 
          console.log("hilfe");



          Then



          const helper = require("./pathofyourfile");





          share|improve this answer



























            2














            What about exporting your function like



            module.exports.help = () => 
            console.log("hilfe");



            Then



            const helper = require("./pathofyourfile");





            share|improve this answer

























              2












              2








              2







              What about exporting your function like



              module.exports.help = () => 
              console.log("hilfe");



              Then



              const helper = require("./pathofyourfile");





              share|improve this answer













              What about exporting your function like



              module.exports.help = () => 
              console.log("hilfe");



              Then



              const helper = require("./pathofyourfile");






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Aug 14 '18 at 6:32









              erdemgunencerdemgunenc

              166514




              166514























                  1














                  @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



                  enter image description here



                  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



                  enter image description here



                  hope it should help you






                  share|improve this answer



























                    1














                    @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



                    enter image description here



                    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



                    enter image description here



                    hope it should help you






                    share|improve this answer

























                      1












                      1








                      1







                      @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



                      enter image description here



                      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



                      enter image description here



                      hope it should help you






                      share|improve this answer













                      @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



                      enter image description here



                      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



                      enter image description here



                      hope it should help you







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Aug 14 '18 at 9:02









                      Damir BeylkhanovDamir Beylkhanov

                      19211




                      19211





















                          1














                          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.






                          share|improve this answer





























                            1














                            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.






                            share|improve this answer



























                              1












                              1








                              1







                              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.






                              share|improve this answer















                              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.







                              share|improve this answer














                              share|improve this answer



                              share|improve this answer








                              edited Mar 22 at 6:33









                              Community

                              11




                              11










                              answered Sep 17 '18 at 22:35









                              Harry WhitehouseHarry Whitehouse

                              155




                              155





















                                  0














                                  ES6/TS



                                  Simply export your function Help in help.ts



                                  export default function Help()


                                  Import anywhere by



                                  import Help from 'help'





                                  share|improve this answer



























                                    0














                                    ES6/TS



                                    Simply export your function Help in help.ts



                                    export default function Help()


                                    Import anywhere by



                                    import Help from 'help'





                                    share|improve this answer

























                                      0












                                      0








                                      0







                                      ES6/TS



                                      Simply export your function Help in help.ts



                                      export default function Help()


                                      Import anywhere by



                                      import Help from 'help'





                                      share|improve this answer













                                      ES6/TS



                                      Simply export your function Help in help.ts



                                      export default function Help()


                                      Import anywhere by



                                      import Help from 'help'






                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Mar 22 at 6:43









                                      BoobalanBoobalan

                                      239110




                                      239110



























                                          draft saved

                                          draft discarded
















































                                          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.




                                          draft saved


                                          draft discarded














                                          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





















































                                          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







                                          Popular posts from this blog

                                          Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

                                          SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

                                          은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현