gpt4 book ai didi

AngularJS:ng-click是 "a good practice"吗?为什么 AngularJS 中没有 ng-{event}?

转载 作者:行者123 更新时间:2023-12-03 05:46:27 24 4
gpt4 key购买 nike

我刚刚开始学习 AngularJS,并且对 AngularJS 的一些概念感到困惑。如果有人能向我解释一些观点,那就太好了......

  1. 我知道 ng-click 在技术上与 onclick 不同(请参阅 here ),但两者都放置在标记中。我认为这将是一种“不好的做法”?如果大多数人说这“不好”,为什么这是 AngularJS 的核心概念之一?我认为最好从 JavaScript 中选择 DOM 元素,而不是在标记中放置逻辑。

  2. 如果 ng-click 是在 AngularJS 中响应点击事件的正确方法,我应该如何处理其他 DOM 事件?我看到还有其他几个指令,例如 ng-mouseover ,但并非所有 DOM 事件都有 AngularJS 等效指令。我将如何处理 AngularJS 中的“drop”事件(因此以 AngularJS 方式 - 不是正常方式)?需要明确的是:我想drag a file into my webapp .

非常感谢,皮波

最佳答案

Why is this one of the core concepts of AngularJS, if most people say this is "bad"?

嗯,真正喜欢Unobtrusive JavaScript的人可能会说这很糟糕。 Angularians(以及那些具有弹性背景的人)看到了更具声明性的方法的值(value)。

"Angular is built around the belief that declarative code is better than imperative when it comes to building UIs and wiring software components together... By declaratively describing how the UI should change as your application state changes, you are freed from low level DOM manipulation tasks. -- Overview doc

另请参阅Don't the data attribute options used in Bootstrap, Angular.js, and Ember.js conflict with Unobtrusive Javascript principles?

what should I do with other DOM events?

编写您自己的指令来监听它们(或者找到其他人已经编写的指令)。 Angular 使您能够通过编写自己的指令来扩展浏览器的功能。

更新:在下面的评论中,Tim Stewart 提到了 AngularUI 的 ui-event ,它允许您将回调绑定(bind)到 Angular 本身不支持的任何事件。

关于AngularJS:ng-click是 "a good practice"吗?为什么 AngularJS 中没有 ng-{event}?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14346073/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com