AngularJS:ng-click 是“一个好习惯"吗?为什么 AngularJS 中没有 ng-{event}? [英] AngularJS: Is ng-click "a good practice"? Why is there no ng-{event} in AngularJS?

查看:21
本文介绍了AngularJS:ng-click 是“一个好习惯"吗?为什么 AngularJS 中没有 ng-{event}?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始学习 AngularJS,并在 AngularJS 的一些概念上挣扎.如果有人可以向我解释一些要点,那就太好了...

I just started to learn AngularJS and struggle with some concepts of AngularJS. It would be great if somebody could explain me some points...

  1. 我了解 ng-click 在技术上与 onclick 不同(请参阅 此处),但两者都放在标记中.我认为这将是一个不好的做法"?为什么这是 AngularJS 的核心概念之一,如果大多数人都说这是糟糕的"?我认为从 JavaScript 中选择 DOM 元素而不是在标记中放置逻辑会更好.

  1. I understand that ng-click is technically not the same as onclick (see here), but both are placed in the markup. I thought that would be a "bad practice"? Why is this one of the core concepts of AngularJS, if most people say this is "bad"? I thought it would be better to select the DOM element from JavaScript and not to place logic in the markup.

如果 ng-click 是对 AngularJS 中的点击事件做出反应的正确方式,我应该如何处理其他 DOM 事件?我看到还有其他几个指令,如 ng-mouseover,但并非所有 DOM 事件都具有 AngularJS 等价物.我将如何处理 AngularJS 中的drop"事件(因此以 AngularJS 方式 - 不是正常方式)?明确地说:我想将文件拖入我的网络应用.

If ng-click is the right way to react to click events in AngularJS what should I do with other DOM events? I see that there are several other directives like ng-mouseover, but not all DOM events have a AngularJS equivalent. How would I handle a 'drop' event in AngularJS (hence in the AngularJS way - not the normal way)? To be clear: I want to drag a file into my webapp.

非常感谢,皮波

推荐答案

为什么这是 AngularJS 的核心概念之一,如果大多数人都说这是糟糕的"?

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

好吧,真正喜欢 Unobtrusive JavaScript 的人可能会说它很糟糕.Angularians(以及那些有 flex 背景的人)看到了更具声明性的方法的价值.

Well, people who really like Unobtrusive JavaScript might say it is bad. Angularians (and those with a flex background) see value in the more declarative approach.

Angular 建立在这样一种信念之上,即在构建 UI 和将软件组件连接在一起时,声明式代码比命令式更好......通过声明式描述 UI 应该如何随着应用程序状态的变化而变化,您可以从低级别 DOM 操作任务.--概述文档

"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

另见 Bootstrap、Angular.js 和 Ember.js 中使用的数据属性选项是否与 Unobtrusive Javascript 原则冲突?

我应该如何处理其他 DOM 事件?

what should I do with other DOM events?

编写您自己的指令以听取他们的意见(或找到其他人已经编写的指令).Angular 使您能够扩展浏览器的功能——通过编写您自己的指令.

Write your own directives to listen for them (or find one that someone else has already written). Angular gives you the power to extend what the browser can do -- by writing your own directives.

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

Update: in the comments below, Tim Stewart mentions AngularUI's ui-event, which lets you bind a callback to any event not natively supported by Angular.

这篇关于AngularJS:ng-click 是“一个好习惯"吗?为什么 AngularJS 中没有 ng-{event}?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆