简体   繁体   English

onclick 标签按钮在角度不起作用

[英]onclick tag button doesn't work in angular

I try to understand why this code doesn't work in angular我试图理解为什么这段代码在 angular 中不起作用

HTML : HTML :

<button onclick="myFunction('hello')">Click me</button>

TYPESCRIPT打字稿

@Component({
  selector: 'app-create',
  templateUrl: './create.component.html',
  styleUrls: ['./create.component.css']
})

export class CreateComponent {

  constructor() {}

}


function myFunction(hello: string) {
  console.log(hello);
}

The error in console when I click单击时控制台中的错误

Uncaught ReferenceError: myFunction is not defined
    at HTMLButtonElement.onclick (create:1)
onclick @ create:1

I know, (click) works perfect, but i need understand why cant use pure javascript in angular project我知道,(单击)效果很好,但我需要了解为什么不能在 angular 项目中使用纯 javascript

PLEASE don't put me -1 I Am learning, Am not perfect =(请不要让我 -1 我正在学习,我并不完美=(

Yes, just like any other event.是的,就像任何其他事件一样。

<button (click)=”handleClick($event)” type=”button”>Button</button>

handleClick(event: Event) {
  console.log(‘Click!’, event)
}

Angular CLI builds your code with webpack under the hood. Angular CLI 在后台使用 webpack 构建您的代码。 Each ts file is transpiled by TypeScript to js and all code in this js file has separated scope.每个 ts 文件都由 TypeScript 转译为 js,并且此 js 文件中的所有代码都有单独的作用域。

So your myFunction is not globally available.所以你的myFunction不是全球可用的。 It is not created as window.myFunction property.它不是作为window.myFunction属性创建的。

Anyway you can try defining that function as a window property manually and this should work:无论如何,您可以尝试将该函数手动定义为窗口属性,这应该可以工作:

window['myFunction'] = function myFunction(hello: string) {
  console.log(hello);
}

Per angular docs每角度文档

When writing a binding, be aware of a template statement's execution context.编写绑定时,请注意模板语句的执行上下文。 The identifiers in a template statement belong to a specific context object, usually the Angular component controlling the template模板语句中的标识符属于特定的上下文对象,通常是控制模板的 Angular 组件

Per MDN web docs每个 MDN 网络文档

The onclick property of the GlobalEventHandlers mixin is the EventHandler for processing click events on a given element. GlobalEventHandlers mixin 的 onclick 属性是用于处理给定元素上的单击事件的 EventHandler。

Conclusion => It comes down to encapsulation or scope.结论 => 归结为封装或范围。 When onclick is used, the browser attempts to find myFunction() in the global context where it does not exist since it is registered in the component scope.当使用 onclick 时,浏览器会尝试在它不存在的全局上下文中查找 myFunction(),因为它是在组件作用域中注册的。 Thus, pure javascript is allowed in angular.因此,在 angular 中允许使用纯 javascript。 Angular is just trying to "help" in this instance.在这种情况下,Angular 只是试图“提供帮助”。

您需要在 createComponent 类中定义 myFunction ,因为当您单击时,将在组件类而不是整个 ts 文件中搜索绑定到事件的函数。

Please use like this;请这样使用;

HTML page HTML页面

<button (click)="myFunction('hello')">Click me</button>

TypeScript - CreateComponent打字稿 - 创建组件

Note : Define myFunction inside the createComponent class注意:在 createComponent 类中定义 myFunction

myFunction(hello: string) {
    console.log(hello);
}

Important : Please note that ,you can write plain old JavaScript in a TypeScript project without any problems since JavaScript is a subset of TypeScript ,you only need some steps if you are planning to use an external JavaScript library with TypeScript.重要提示:请注意,由于 JavaScript 是 TypeScript 的一个子集,因此您可以在 TypeScript 项目中编写普通的旧 JavaScript,没有任何问题,如果您计划将外部 JavaScript 库与 TypeScript 一起使用,则只需执行一些步骤。

Read this : https://www.techiediaries.com/use-external-javascript-libraries-typescript-projects/阅读: https : //www.techiediaries.com/use-external-javascript-libraries-typescript-projects/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM