ReferenceError在Angular中让我疯狂

huangapple go评论94阅读模式
英文:

ReferenceError in Angular driving me crazy

问题

I am working on a program and I am implementing a function with click. Now it has been a while since I did Angular, but this referenceError is driving me crazy. I have a function.

  1. dosomeshit(stringelement: string): void{
  2. console.log("reached function: " + stringelement);
  3. }

which is activated by this button.

  1. <div class="flexcontainer">
  2. <div class="card" style="width: 18rem; margin-left: 2%;">
  3. <div class="card-header">
  4. <h2>Overzicht:</h2>
  5. </div>
  6. <ul class="list-group list-group-flush">
  7. <li class="list-group-item" *ngFor="let stringelement of elements">
  8. <div class="flexinsideli">
  9. <div style="width: 90%;">
  10. <p> {{stringelement}} </p>
  11. </div>
  12. <div>
  13. <button type="button" onclick="dosomeshit(stringelement)">
  14. <fa-icon class="fa-1x" [icon]="faPlus"></fa-icon>
  15. </button>
  16. </div>
  17. </div>
  18. </li>
  19. </ul>
  20. <div class="login-alert-msg" *ngIf="errorMessageList">{{errorMessageList}}</div>
  21. </div>
  22. </div>

now I keep getting a referenceError: 14 Uncaught ReferenceError: dosomeshit is not defined when I use click nothing happens at all. Why is my function not reached when I click the button? Why would dosomeshit not be defined? I did define it in my ts file.

英文:

I am working on a program and I am implementing a function with click. Now it has been a while since I did Angular, but this referenceError is driving me crazy. I have a function.

  1. dosomeshit(stringelement: string): void{
  2. console.log(&quot;reached function: &quot; + stringelement);
  3. }

which is activated by this button.

  1. &lt;div class=&quot;flexcontainer&quot;&gt;
  2. &lt;div class=&quot;card&quot; style=&quot;width: 18rem; margin-left: 2%;&quot;&gt;
  3. &lt;div class=&quot;card-header&quot;&gt;
  4. &lt;h2&gt;Overzicht:&lt;/h2&gt;
  5. &lt;/div&gt;
  6. &lt;ul class=&quot;list-group list-group-flush&quot;&gt;
  7. &lt;li class=&quot;list-group-item&quot; *ngFor=&quot;let stringelement of elements&quot;&gt;
  8. &lt;div class=&quot;flexinsideli&quot;&gt;
  9. &lt;div style=&quot;width: 90%;&quot;&gt;
  10. &lt;p&gt; {{stringelement}} &lt;/p&gt;
  11. &lt;/div&gt;
  12. &lt;div&gt;
  13. &lt;button type=&quot;button&quot; onclick=&quot;dosomeshit(stringelement)&quot;&gt;
  14. &lt;fa-icon class=&quot;fa-1x&quot; [icon]=&quot;faPlus&quot;&gt;&lt;/fa-icon&gt;
  15. &lt;/button&gt;
  16. &lt;/div&gt;
  17. &lt;/div&gt;
  18. &lt;/li&gt;
  19. &lt;/ul&gt;
  20. &lt;div class=&quot;login-alert-msg&quot; *ngIf=&quot;errorMessageList&quot;&gt;{{errorMessageList}}&lt;/div&gt;
  21. &lt;/div&gt;

now I keep getting a referenceError: 14 Uncaught ReferenceError: dosomeshit is not defined
at HTMLButtonElement.onclick ( ..

when I use click nothing happens at all. Why is my function not reached when I click the button? Why would dosomeshit not be defined? I did define it in my ts file.

答案1

得分: 0

使用(click)而不是onclickclick。它应该能够正常工作。这是在Angular中进行事件绑定的正确语法。

英文:

Instead of onclick or click use (click). It should work. This is the right syntax in Angular for event binding.

huangapple
  • 本文由 发表于 2023年5月26日 16:15:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76338930.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定