AngularJS vs. jQuery
AngularJS and jQuery adopt very different ideologies. If you're coming from jQuery you may find some of the differences surprising. AngularJS may make you angry.This is normal, you should push through. AngularJS is worth it.
First up, AngularJS doesn't replace jQuery
AngularJS and jQuery do different things. AngularJS gives you a set of tools to produce webapps. jQuery mainly gives you tools for modifying the DOM. If jQuery is present on your page, AngularJS will use it automatically. If it isn't, AngularJS ships with jQuery Lite, which is a cut down, but still perfectly usable version of jQuery.Misko likes jQuery and encourages you to use it. You still need to manipulate the DOM and jQuery is your tool for this. However you can get a most of your work done using templates, and you should prefer templates where possible.
People encouraging you to drop jQuery should stop encouraging you to do that. You might like to lay off the jQuery for a while while you learn what AngularJS can do, but jQuery is not going away just yet.
That said, you shouldn't be sprinkling jQuery all over the place. The correct place for jQuery and other DOM manipulations in AngularJS is in directives. More on these later.
Unobtrusive JavaScript vs. Declarative Templates
jQuery is typically applied unobtrusively. Your JavaScript code is linked in the header, and this is the only place it is mentioned. The JavaScript code wraps round the DOM like a snail on a twig, making changes as required. Onclick attributes are very bad practice.One of the first things your will notice about AngularJS is that custom attributes are everywhere. Your HTML will be littered with ng attributes, which are essentially onClick attributes on steroids. These are directives, and are one of the main ways in which the template is hooked to the model.
When you first see this you might be tempted to write AngularJS off as old school intrusive JavaScript (like I did at first). In fact, AngularJS does not play by those rules. In AngularJS, your HTML5 is a template. It is compiled by AngularJS to produce your web page.
This is the first big difference. To jQuery, your web page is a DOM to be manipulated. To AngularJS, your HTML is code to be compiled. AngularJS reads in your whole web page and literally compiles it into a new web page using it's built in compiler.
Your template should be declarative; it's meaning should be clear simply by reading it. We use custom attributes with meaningful names. We make up new HTML elements, again with meaningful names. A designer with minimal HTML knowledge and no coding skill can read your AngularJS template and understand what it is doing. He or she can make modifications. This is the AngularJS way.
The template is in the driving seat.
One of the first questions I asked myself when starting AngularJS and running through the tutorials is "Where is my code?". I've written no JavaScript, and yet I have all this behaviour. The answer is obvious. Because AngularJS compliles the DOM, AngularJS is treating your HTML as code. For simple cases it's often sufficient to just write a template and let AngularJS compile it into an application for you.Your template drives your application. It's treated as a DSL. You write AngularJS components, and AngularJS will take care of pulling them in and making them available at the right time based on the structure of your template. This is very different to a standard MVC pattern, where the template is just for output.
It's more similar to XSLT than Ruby on Rails for example.
Semantic HTML vs. Semantic Models
With jQuery your HTML page should contain semantic meaningful content. If the JavaScript is turned off (by a user or search engine) your content remains accessible.Because AngularJS treats your HTML page as a template. The template is not supposed to be semantic as your content is typically stored in your model. AngularJS compiles your DOM with the model to produce a semantic web page.
In AngularJS, meaning lives in the model, the HTML is for display only.
At this point you likely have all sorts of questions concerning SEO and accessibility, and rightly so. There are open issues here. Most screen readers will now parse JavaScript. Search engines may also be able to index Ajaxed content. Nevertheless, you will want to make sure you are using pushstate URLs and you have a decent sitemap. See here for a discussion of the issue: http://stackoverflow.com/a/23245379/687677
Separation of concerns vs. MVC
Separation of concerns is a pattern that has grown up over many years of web development for a variety of reasons including SEO, accessibility and browser incompatibility. It looks like this:- HTML - Semantic meaning. The HTML should stand alone.
- CSS - Styling, without the CSS the page is still readable.
- JavaScript - Behaviour, without the script the content remains.
- Model - your models contains your semantic data. Models are usually JSON objects.
- View - Your views are written in HTML. The view is usually not semantic because your data lives in the model.
- Controller - Your controller is a JavaScript function which hooks the view to the model. Depending on your app, you may or may not need to create a controller. You can have many controllers on a page.
Plugins vs. Directives
Plugins extend jQuery. AngularJS Directives extend the capabilities of your browser.In jQuery we define plugins by adding functions to the jQuery.prototype. We then hook these into the DOM by selecting elements and calling the plugin on the result. The idea is to extend the capabilities of jQuery.
For example, if you want a carousel on your page, you might define an unordered list of figures, perhaps wrapped in a nav element. You might then write some jQuery to find the list on the page and restyle it as a gallery with timeouts to do the sliding animation.
In AngularJS, we define directives. A directive is a function which returns a JSON object. This object tells AngularJS what DOM elements to look for, and what changes to make to them. Directives are hooked in to the template using either attributes or elements, which you invent. The idea is to extend the capabilities of HTML with new attributes and elements.
The AngularJS way is to extend the capabilities of native looking HTML. You should write HTML that looks like HTML, extended with custom attributes and elements.
If you want a carousel, just use a
<carousel />
element, then define a directive to pull in a template, and make that sucker work.Closure vs. $scope
jQuery plugins are created in a closure. Privacy is maintained within that closure. It's up to you to maintain your scope chain within that closure. You only really have access to the set of DOM nodes passed in to the plugin by jQuery, plus any local variables defined in the closure and any globals you have defined. This means that plugins are quite self contained. This is a good thing, but can get restrictive when creating a whole application. Trying to pass data between sections of a dynamic page becomes a chore.AngularJS has $scope objects. These are special objects created and maintained by AngularJS in which you store your model. Certain directives will spawn a new $scope, which by default inherits from its wrapping $scope using JavaScript prototypical inheritance. The $scope object is accessible in the controller and the view.
This is the clever part. Because the structure of $scope inheritance roughly follows the structure of the DOM, elements have access to their own scope, and any containing scopes seamlessly, all the way up to the global $scope (which is not the same as the global scope).
This makes it much easier to pass data around, and to store data at an appropriate level. If a dropdown is unfolded, only the dropdown $scope needs to know about it. If the user updates their preferences, you might want to update the global $scope, and any nested scopes listening to the user preferences would automatically be alerted.
This might sound complicated, in fact, once you relax into it, it's like flying. You don't need to create the $scope object, Angular instantiates and configures it for you, correctly and appropriately based on your template hierarchy. Angular then makes it available to your component using the magic of dependency injection (more on this later).
Manual DOM changes vs. Data Binding
In jQuery you make all your DOM changes by hand. You construct new DOM elements programatically. If you have a JSON array and you want to put it to the DOM, you must write a function to generate the HTML and insert it.In AngularJS you can do this too, but you are encouraged to make use of data binding. Change your model, and because the DOM is bound to it via a template your DOM will automatically update, no intervention required.
Because data binding is done from the template, using either an attribute or the curly brace syntax, it's super easy to do. There's little cognitive overhead associated with it so you'll find yourself doing it all the time.
<input ng-model="user.name" />
Binds the input element to $scope.user.name
. Updating the input will update the value in your current scope, and vice-versa.Likewise:
<p>
{{user.name}}
</p>
will output the user name in a paragraph. It's a live binding, so if the $scope.user.name
value is updated, the template will update too.Ajax all of the time
In jQuery making an Ajax call is fairly simple, but it's still something you might think twice about. There's the added complexity to think about, and a fair chunk of script to maintain.In AngularJS, Ajax is your default go-to solution and it happens all the time, almost without you noticing. You can include templates with ng-include. You can apply a template with the simplest custom directive. You can wrap an Ajax call in a service and create yourself a GitHub service, or a Flickr service, which you can access with astonishing ease.
Service Objects vs Helper Functions
In jQuery, if we want to accomplish a small non-dom related task such as pulling a feed from an API, we might write a little function to do that in our closure. That's a valid solution, but what if we often want to access that feed often? What if we want to reuse that code in another application?AngularJS gives us service objects.
Services are simple objects that contain functions and data. They are always singletons, meaning there can never be more than one of them. Say we want to access the Stack Overflow API, we might write a
StackOverflowService
which defines methods for doing so.Let's say we have a shopping cart. We might define a ShoppingCartService which maintains our cart and contains methods for adding and removing items. Because the service is a singleton, and is shared by all other components, any object that needs to can write to the shopping cart and pull data from it. It's always the same cart.
Service objects are self-contained AngularJS components which we can use and reuse as we see fit.
Dependency injection (DI)
DI is a massive deal in AngularJS. It means that AngularJS will automatically instantiate your objects for you and ensure they are available for you where you need them. Until you start to use this, it's hard to explain just what a massive time boon is. Nothing like AngularJS DI exists inside jQuery.DI means that instead of writing your application and wiring it together, you instead define a library of components, each identified by a string.
Say I have a component called 'FlickrService' which defines methods for pulling JSON feeds from Flickr. Now, if I want to write a controller that can access Flickr, I just need to refer to the 'FlickrService' by name when I declare the controller. AngularJS will take care of instantiating the component and making it available to my controller.
For example, here I define a service:
myApp.service('FlickrService', function() {
return {
getFeed: function() { // do something here }
}
});
Now when I want to use that service I just refer to it by name like this:myApp.controller('myController', ['FlickrService', function(FlickrService) {
FlickrService.getFeed()
}]);
Angular will recognise that a FlickrService object is needed to instantiate the controller, and will provide one for us.This makes wiring things together very easy, and pretty much eliminates any tendency towards spagettification.
Modular service architecture
jQuery says very little about how you should organise your code. AngularJS has opinions.AngularJS gives you modules into which you can place your code. If you're writing a script that talks to Flickr for example, you might want to create a Flickr module to wrap all your Flickr related functions in. Modules can include other modules (DI). Your main application is usually a module, and this should include all the other modules your application will depend on.
You get simple code reuse, if you want to write another application based on Flickr, you can just include the Flickr module and voila, you have access to all your Flickr related functions in your new application.
Modules contain AngularJS components. When we include a module, all the components in that module become available to us as a simple list identified by their unique strings. We can then inject those components into each other using AngularJS's dependency injection mechanism.
To sum up
AngularJS and jQuery are not enemies. It's possible to use jQuery within AngularJS very nicely. If you're using AngularJS well (templates, data-binding, $scope, directives, etc.) you will find you need a lot less jQuery than you might otherwise require.Think less about unobtrusive JavaScript, and instead think in terms of HTML extension.
No comments :
Post a Comment