Uncaught ReferenceError: is not defined - InnerHTML

` – naman1901 Oct 24 '18 at 14:07
  • in which file you have added ` – Aagam Jain Oct 24 '18 at 14:13
  • I tried to put the script on top of my HTML file, still not working. Any pointers on how to make it global? – naman1901 Oct 26 '18 at 06:44
  • It won't be right to put it in index.html in my opinion. The function is used only in a specific place. – naman1901 Oct 26 '18 at 06:52
  • 3 Answers3

    0

    Angular looks for code in the component's typescript file, so I think you'd need to put the code there. You can create a service to get the code from the backend, then call the service from your typescript file and use a (click) event to call the typescript code.

    megabc123
    • 200
    • 1
    • 5
    0

    You need to change your code like this:

    <a (click)="ToggleDisplayMode()"><u>show/hide</u></a>
    

    Notice that instead of onclick, you have to use (click)

    Pankaj Shukla
    • 2,657
    • 2
    • 11
    • 18
    • That is an Angular solution - changes at that level will not be possible as this HTML is returned by an SDK and the same response is used in other, non-Angular interfaces – naman1901 Oct 24 '18 at 14:02
    0

    Angular version 9.1.9 typescript 3.8.3

    You can add a js file under assets folder

    src/assets/js/child2.js

    function javaChangeCreatedBy() {
        alert('Set 2 clicked');
    
        var i_createdby = document.getElementById('inputcreatedbyid')
        var i_value = document.getElementById('inputcreatedbyid').value;
    }
    

    Following html code shows the both approach one for angular click and one for html onclick.

    src/app/first-component/first-component.component.html

    <p>
        Enter Created by:<input id='inputcreatedbyid' type="text" placeholder="Enter name" [(ngModel)]=inputcreatedby>
        &nbsp;
        <button type="button" (click)="ChangeCreatedBy(inputcreatedby)">Set</button> 
        &nbsp;
        <button type="button" onclick="javaChangeCreatedBy()">Set 2</button>
    </p>
    

    src/app/first-component/first-component.component.ts

     export class FirstComponentComponent implements OnInit {
          ..
          createdby = 'My name';
          inputcreatedby='';
    
          ChangeCreatedBy(inputcreatedby:string){
               // var inputvalue = document.getElementById("inputcreatedby").value;
               this.createdby = inputcreatedby;
          }
     }
    

    And finally add the script file into angular.json file

     {
        "projects": {
           "architect": {
               "build": {
                  ..
                  "options": {
                     ..
                     "scripts": [
                        "src/assets/js/child2.js"
                     ]
                  }
    

    However angular doesn't support passing type script parameter to java function.

    Kagan Agun
    • 489
    • 1
    • 6
    • 7