[Javascript] "التفكير في AngularJS" إذا كان لدي خلفية jQuery؟


لا بد من → التعريفي

في jQuery ، يتم استخدام المحددات للعثور على عناصر DOM ثم ربط / تسجيل معالجات الأحداث لهم. عند تشغيل حدث ما ، ينفذ هذا الرمز (الإلزامي) لتحديث / تغيير DOM.

في AngularJS ، تريد التفكير في طرق العرض بدلاً من عناصر DOM. المشاهدات هي (افتراضية) HTML التي تحتوي على توجيهات AngularJS. تضع التوجيهات معالجات الأحداث خلف الكواليس لنا وتعطينا ربطًا ديناميكيًا. نادرا ما يتم استخدام المختارين ، وبالتالي فإن الحاجة إلى المعرفات (وبعض أنواع الطبقات) تتضاءل إلى حد كبير. ترتبط المشاهدات بالنماذج (عبر النطاقات). وجهات النظر هي إسقاط للنموذج. نماذج تغيير الأحداث (أي ، البيانات ، خصائص النطاق) ، والمشاهدات التي تعرض تحديث هذه النماذج "تلقائيًا".

في AngularJS ، فكر في الموديلات ، بدلاً من عناصر DOM المحددة في jQuery والتي تمسك ببياناتك. فكر في المشاهدات كإسقاطات لهذه النماذج ، بدلاً من تسجيل طلبات الاسترداد للتلاعب بما يراه المستخدم.

فصل المخاوف

jQuery توظف جافا سكريبت غير مزعجة - يتم فصل سلوك (JavaScript) من الهيكل (HTML).

يستخدم AngularJS أجهزة التحكم والتوجيهات (كل منها يمكن أن يكون جهاز التحكم الخاص به ، و / أو ترجمة وربط الوظائف) لإزالة السلوك من العرض / البنية (HTML). لدى Angular أيضًا خدمات ومرشحات للمساعدة في فصل / تنظيم طلبك.

انظر أيضًا https://stackoverflow.com/a/14346528/215945

تصميم التطبيق

نهج واحد لتصميم تطبيق AngularJS:

  1. فكر في نماذجك. إنشاء خدمات أو كائنات JavaScript الخاصة بك لهذه النماذج.
  2. فكر في الطريقة التي تريد بها تقديم نماذجك - وجهات نظرك. قم بإنشاء قوالب HTML لكل طريقة عرض ، باستخدام الأوامر الضرورية للحصول على ربط بيانات ديناميكي.
  3. يمكنك إرفاق وحدة تحكم بكل عرض (باستخدام عرض ng والتوجيه أو ng-controller). اطلب من وحدة التحكم العثور على / الحصول فقط على أي بيانات نموذجية يحتاجها العرض للقيام بعمله. جعل وحدات التحكم رقيقة قدر الإمكان.

الوراثة البدائية

يمكنك القيام بالكثير مع jQuery دون معرفة كيفية عمل الوراثة النموذجية لجافا سكريبت. عند تطوير تطبيقات AngularJS ، سوف تتجنب بعض العثرات الشائعة إذا كان لديك فهم جيد لميراث JavaScript. القراءة الموصى بها: ما هي الفروق الدقيقة في الميراث النموذجي / النموذجي في AngularJS؟


لنفترض أنني على دراية بتطوير تطبيقات من جانب العميل في jQuery ، لكن الآن أود البدء باستخدام AngularJS . هل يمكنك وصف التحول الفكري الذي هو ضروري؟ إليك بعض الأسئلة التي قد تساعدك في صياغة إجابة:

  • كيف يمكنني تصميم وتصميم تطبيقات الويب من جانب العميل بشكل مختلف؟ ما هو الفرق الأكبر؟
  • ماذا يجب أن أتوقف عن فعل / استخدام ؛ ما الذي يجب أن أبدأ به / استخدمه بدلاً من ذلك؟
  • هل هناك أي اعتبارات / قيود على جانب الخادم؟

أنا لا أبحث عن مقارنة مفصلة بين jQuery و AngularJS .


jQuery makes ridiculously long JavaScript commands like getElementByHerpDerp shorter and cross-browser.


AngularJS allows you to make your own HTML tags/attributes that do things which work well with dynamic web applications (since HTML was designed for static pages).


Saying "I have a jQuery background how do I think in AngularJS?" is like saying "I have an HTML background how do I think in JavaScript?" The fact that you're asking the question shows you most likely don't understand the fundamental purposes of these two resources. This is why I chose to answer the question by simply pointing out the fundamental difference rather than going through the list saying "AngularJS makes use of directives whereas jQuery uses CSS selectors to make a jQuery object which does this and that etc....". This question does not require a lengthy answer.

jQuery is a way to make programming JavaScript in the browser easier. Shorter, cross-browser commands, etc.

AngularJS extends HTML, so you don't have to put <div> all over the place just to make an application. It makes HTML actually work for applications rather than what it was designed for, which is static, educational web pages. It accomplishes this in a roundabout way using JavaScript, but fundamentally it is an extension of HTML, not JavaScript.

As a JavaScript MV* beginner and purely focusing on the application architecture (not the server/client-side matters), I would certainly recommend the following resource (which I am surprised wasn't mentioned yet): JavaScript Design Patterns , by Addy Osmani, as an introduction to different JavaScript Design Patterns . The terms used in this answer are taken from the linked document above. I'm not going to repeat what was worded really well in the accepted answer. Instead, this answer links back to the theoretical backgrounds which power AngularJS (and other libraries).

Like me, you will quickly realize that AngularJS (or Ember.js , Durandal, & other MV* frameworks for that matter) is one complex framework assembling many of the different JavaScript design patterns.

I found it easier also, to test (1) native JavaScript code and (2) smaller libraries for each one of these patterns separately before diving into one global framework. This allowed me to better understand which crucial issues a framework adresses (because you are personally faced with the problem).


  • JavaScript Object-oriented Programming (this is a Google search link). It is not a library, but certainly a prerequisite to any application programming. It taught me the native implementations of the prototype, constructor, singleton & decorator patterns
  • jQuery / Underscore for the facade pattern (like WYSIWYG's for manipulating the DOM)
  • Prototype.js for the prototype/ constructor/ mixin pattern
  • RequireJS / Curl.js for the module pattern/ AMD
  • KnockoutJS for the observable, publish/subscribe pattern

NB: This list is not complete, nor 'the best libraries'; they just happen to be the libraries I used. These libraries also include more patterns, the ones mentioned are just their main focuses or original intents. If you feel something is missing from this list, please do mention it in the comments, and I will be glad to add it.

I find this question interesting, because my first serious exposure to JavaScript programming was Node.js and AngularJS. I never learned jQuery, and I guess that's a good thing, because I don't have to unlearn anything. In fact, I actively avoid jQuery solutions to my problems, and instead, solely look for an "AngularJS way" to solve them. So, I guess my answer to this question would essentially boil down to, "think like someone who never learned jQuery" and avoid any temptation to incorporate jQuery directly (obviously AngularJS uses it to some extent behind the scenes).

jQuery is a DOM manipulation library.

AngularJS is an MV* framework.

In fact, AngularJS is one of the few JavaScript MV* frameworks (many JavaScript MVC tools still fall under the category library).

Being a framework, it hosts your code and takes ownership of decisions about what to call and when!

AngularJS itself includes a jQuery-lite edition within it. So for some basic DOM selection/manipulation, you really don't have to include the jQuery library (it saves many bytes to run on the network.)

AngularJS has the concept of "Directives" for DOM manipulation and designing reusable UI components, so you should use it whenever you feel the need of doing DOM manipulation related stuff (directives are only place where you should write jQuery code while using AngularJS).

AngularJS involves some learning curve (more than jQuery :-).

-->For any developer coming from jQuery background, my first advice would be to "learn JavaScript as a first class language before jumping onto a rich framework like AngularJS!" I learned the above fact the hard way.

حظا طيبا وفقك الله.

Can you describe the paradigm shift that is necessary?

Imperative vs Declarative

With jQuery you tell the DOM what needs to happen, step by step. With AngularJS you describe what results you want but not how to do it. More on this here . Also, check out Mark Rajcok's answer.

How do I architect and design client-side web apps differently?

AngularJS is an entire client-side framework that uses the MVC pattern (check out their graphical representation ). It greatly focuses on separation of concerns.

What is the biggest difference? What should I stop doing/using; what should I start doing/using instead?

jQuery is a library

AngularJS is a beautiful client-side framework, highly testable, that combines tons of cool stuff such as MVC, dependency injection , data binding and much more.

It focuses on separation of concerns and testing ( unit testing and end-to-end testing), which facilitates test-driven development.

The best way to start is going through their awesome tutorial . You can go through the steps in a couple of hours; however, in case you want to master the concepts behind the scenes, they include a myriad of reference for further reading.

Are there any server-side considerations/restrictions?

You may use it on existing applications where you are already using pure jQuery. However, if you want to fully take advantage of the AngularJS features you may consider coding the server side using a RESTful approach.

Doing so will allow you to leverage their resource factory , which creates an abstraction of your server side RESTful API and makes server-side calls (get, save, delete, etc.) incredibly easy.

They're apples and oranges. You don't want to compare them. They're two different things. AngularJs has already jQuery lite built in which allows you to perform basic DOM manipulation without even including the full blown jQuery version.

jQuery is all about DOM manipulation. It solves all the cross browser pain otherwise you will have to deal with but it's not a framework that allows you to divide your app into components like AngularJS.

A nice thing about AngularJs is that it allows you to separate/isolate the DOM manipulation in the directives. There are built-in directives ready for you to use such as ng-click. You can create your own custom directives that will contain all your view logic or DOM manipulation so you don't end up mingle DOM manipulation code in the controllers or services that should take care of the business logic.

Angular breaks down your app into - Controllers - Services - Views - etc.

and there is one more thing, that's the directive. It's an attribute you can attach to any DOM element and you can go nuts with jQuery within it without worrying about your jQuery ever conflicts with AngularJs components or messes up with its architecture.

I heard from a meetup I attended, one of the founders of Angular said they worked really hard to separate out the DOM manipulation so do not try to include them back in.