简体   繁体   English

如果不是指令,Angular 2中的(点击)是什么?

[英]What is (click) in Angular 2, if not a directive?

In the docs of Angular 2 it's written: 在Angular 2的文档中写道:

Why create a directive to handle a click when we can write a simple binding such as this? 当我们可以编写这样的简单绑定时,为什么要创建一个指令来处理单击? <button (click)="onSave()">Save</button>

If this isn't a directive, what is it then? 如果这不是指令,那是什么? They call it "a simple binding", how is this different from a directive? 他们称其为“简单绑定”,这与指令有何不同? I mean it's still something special handled by Angular, so why are they so proud that it's (click) instead of ng-click like in Angular 1? 我的意思是Angular仍然要处理一些特殊的事情,所以为什么他们如此自豪以至于它不是Angular 1中的ng-click而是(click)

It is my understanding that the parentheses (event) syntax is NOT a directive, but a binding syntax that allows the developer to use DOM events without having to write a directive. 据我了解,括号(event)语法不是指令,而是绑定语法 ,使开发人员无需编写指令即可使用DOM事件。 Whereas, ng-click is a directive, of course. ng-click当然是指令。

With this new binding syntax, there is no need to code a directive to take advantage of DOM events. 使用这种新的绑定语法,无需编写指令即可利用DOM事件。 There are a lot of DOM events , so not having to maintain a directive for all of them is nice. 很多 DOM事件 ,因此不必为所有事件都保留指令是一件好事。 And something to be proud of, IMO. 值得骄傲的是,IMO。

I hope this helps you out. 我希望这能够帮到你。

From my understanding, the rules around binding are such that Angular (2) doesn't even know anything about explicit about click. 根据我的理解,绑定的规则是如此之类,以至于Angular(2)甚至都不知道有关单击的显式信息。 The runtime can't find anything else (directive, component) named "click", so it just adds in a DOM click property. 运行时找不到名为“ click”的其他任何东西(指令,组件),因此它仅添加了DOM click属性。 Because it is (click) and not [click], it adds it in as onclick and then your browser knows what to do with it. 因为它是(单击)而不是[单击],所以将其添加为onclick,然后您的浏览器便知道该如何处理。

It threw me for a loop at first because,coming from AngularJS (1.x), I was trying to find in the API reference for what DOM things they aliased (like click). 一开始它让我陷入了一个循环,因为从AngularJS(1.x)开始,我试图在API参考中查找它们别名的DOM内容(例如click)。 Then I realized they didn't need to alias the DOM with their own directives when I read this carefully https://angular.io/docs/ts/latest/guide/template-syntax.html 然后我意识到当我仔细阅读以下内容时,他们不需要使用自己的指令为DOM别名:https://angular.io/docs/ts/latest/guide/template-syntax.html

Or I'm completely missing the boat, which wouldn't surprise me. 或我完全想念这艘船,这不会令我惊讶。

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

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