[英]See the Angular 6 click bindings in the developer console while in dev mode?
Assume I have a button with the following attributes: 假设我有一个具有以下属性的按钮:
<button>
class="button"
(click)="doStuff()"
[ngClass]="{'stuff': selected}">
Push me
</button>
If I go to the console while running the app in development more, I only get a list of ng-reflect attributes that refer to my added attributes. 如果在开发环境中运行该应用程序的同时进入控制台,则只会得到一个ng-reflect属性列表,这些属性引用了我添加的属性。
Question is: why don't I also see the (click) binding i just added? 问题是:为什么我也看不到我刚刚添加的(单击)绑定?
Because your code is Typescript code, and when you want to look at it into your browser, it's Javascript code. 因为您的代码是Typescript代码,所以当您想在浏览器中查看它时,它就是Javascript代码。
When you write 当你写
<button>
class="button"
(click)="doStuff()"
[ngClass]="{'stuff': selected}">
Push me
</button>
This piece of HTML is actually translated in a string, that is used in the javascript code. 这段HTML实际上被翻译成一个字符串,该字符串在javascript代码中使用。
The compiler then search for (click)
, and replace this code with ng-reflect="doStuff()"
(this might not be true, but this is the idea). 然后,编译器搜索
(click)
,并用ng-reflect="doStuff()"
替换此代码(这可能不正确,但这是个主意)。
It then binds an event with button.addEventListener('click', () => {...})
, append the replaced string to the template, and your button then accepts click events. 然后,
button.addEventListener('click', () => {...})
一个事件与button.addEventListener('click', () => {...})
绑定,将替换后的字符串附加到模板,然后您的按钮接受click事件。
This is an idea of how Angular works. 这是Angular的工作原理。 If you want to know exactly how it does that, you can look at their source code.
如果您想确切地知道它是如何做到的,可以查看它们的源代码。
But bottom line is : (click)
isn't valid JS on a HTML tag, it works because of the compiled code the framework provides when you build your application. 但最重要的是:(
(click)
不是HTML标记上的有效JS,它之所以起作用是因为框架在您构建应用程序时提供了已编译的代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.