使用 AngularJS 和 JQuery 的用例
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15078487/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Use cases for using AngularJS and JQuery
提问by arcdegree
I'm a JQuery programmer looking to get into using AngularJS for my next project. After reading this excellent answerabout how to "think in Angular" instead of including and using JQuery, I wonder what good use cases exist for using JQuery + Angular. I'd like to bring some small amount of familiarity to my Angular programming.
我是一名 JQuery 程序员,希望在我的下一个项目中使用 AngularJS。在阅读了关于如何“在 Angular 中思考”而不是包含和使用 JQuery 的优秀答案后,我想知道使用 JQuery + Angular 有哪些好的用例。我想稍微熟悉一下我的 Angular 编程。
Quoting from the Stackoverflow post:
引用 Stackoverflow 的帖子:
"The bottom line is this: when solutioning, first "think in AngularJS"; if you can't think of a solution, ask the community; if after all of that there is no easy solution, thenfeel free to reach for the jQuery"
“底线是这样的:解决时,首先“在 AngularJS 中思考”;如果您想不出解决方案,请询问社区;如果毕竟没有简单的解决方案,那么请随时联系 jQuery ”
When should I reach for JQuery?
我应该什么时候使用 JQuery?
采纳答案by Coder1
I think the key words in the quote of your question are "*if after all that there is no easysolution*"
我认为你的问题引用中的关键词是“*如果毕竟没有简单的解决方案*”
I wonder what good use cases exist for using JQuery + Angular.
我想知道使用 JQuery + Angular 有哪些好的用例。
Use Case: A project I'm working on uses Zurb Foundation's "reveal" component for a modal window, which I particularly like. It compliments the app I'm working on well. Sure there is an Angular UI project that features a modal window, but I preferred reveal in this case.
用例:我正在从事的一个项目使用 Zurb Foundation 的“显示”组件作为模态窗口,我特别喜欢它。它赞美了我正在开发的应用程序。当然有一个 Angular UI 项目具有模态窗口,但我更喜欢在这种情况下显示。
This has a dependency on JQuery. User blesh answered with great ideas on how to incorporate this the AngularJS way here.
这依赖于 JQuery。用户 blesh 给出了关于如何以 AngularJS 的方式在这里合并的好主意。
After some research, if you conclude that you can do something a lot easier in JQuery, then do it, but be sure to integrate the component the AngularJS way.
经过一些研究,如果你得出结论,你可以在 JQuery 中做一些更容易的事情,那就去做吧,但一定要以 AngularJS 的方式集成组件。
That is my take on it anyway.
无论如何,这就是我的看法。
回答by Neil
AngularJS is a complex framework with a moderate to steep learning curve. JQuery is not a framework but a library. If you are looking to build a single page app especially a CRUD app then AngularJS is a good fit as it will offer two way data binding, routing (important for single page apps), plus many other features out the box.
AngularJS 是一个复杂的框架,具有中等到陡峭的学习曲线。JQuery 不是框架而是库。如果您希望构建单页应用程序,尤其是 CRUD 应用程序,那么 AngularJS 非常适合,因为它将提供双向数据绑定、路由(对于单页应用程序很重要)以及许多其他开箱即用的功能。
It uses jQlite a subset of jQuery, but it is opinionated about how apps are built, this means you need a solid understanding of how it works, you will need to understand controllers, services and directives and why you should only update the DOM in directives.
它使用 jQlite 的一个 jQuery 子集,但它对应用程序的构建方式有偏见,这意味着您需要深入了解它的工作原理,您将需要了解控制器、服务和指令以及为什么您应该只更新指令中的 DOM .
Single page apps can be done using jQuery alone but you will have to manage the framework side of things yourself.
单页应用程序可以单独使用 jQuery 来完成,但您必须自己管理框架方面的事情。
If you include jQuery before AngularJS it will substituted for jQlite, to get to grips with the fundamentals, and thus arm yourself with knowledge to make decisions on its use, have a look at these excellent videos at egghead.io.
如果您在 AngularJS 之前包含 jQuery,它将取代 jQlite,以掌握基础知识,从而用知识武装自己来决定其使用,请查看egghead.io上的这些优秀视频。
When to reach for jQuery
何时使用 jQuery
You reach for jQuery when you cannot do something the Angular way, this is often when you are at the boundary of the framework.
当您无法以 Angular 的方式做某事时,您会使用 jQuery,这通常是当您处于框架的边界时。
Examples:
例子:
An example of this is Angular doesn't have a way to disable items in a select element. You would reach for jQuery here to listen for a change event on the select and manipulate the UI yourself from a directive. See update below.
这方面的一个例子是 Angular 没有办法禁用选择元素中的项目。您可以在此处使用 jQuery 来侦听选择上的更改事件,并通过指令自己操作 UI。请参阅下面的更新。
If you are using a third party library for example, you may need to trigger an event using jQuery and pick it up in the third party code or elsewhere. Here you will find jQuery or something like pubsub invaluable.
例如,如果您使用第三方库,则可能需要使用 jQuery 触发事件并在第三方代码或其他地方获取它。在这里,您会发现 jQuery 或类似 pubsub 之类的东西非常宝贵。
UPDATE
更新
Regarding the routing capabilities, I strongly recommend the excellent UI-Routerinstead of the out of the box router.
关于路由能力,我强烈推荐优秀的UI-Router而不是开箱即用的路由器。
UPDATE v2
更新 v2
Angular 1.4 now has support for disabling options in a select element.
Angular 1.4 现在支持禁用选择元素中的选项。