Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
December 26, 2012 09:51 pm GMT

3 Reasons to Choose AngularJS for Your Next Project

AngularJS is a relatively new JavaScript framework by Google, designed to make your front-end development as easy as possible. There are plenty of frameworks and plugins available. As such, it can sometimes prove difficult to sift through all of the noise to find useful tools.

Here are three reasons why you might choose AngularJS for your next project.


1 – It Was Developed by Google

Angular is built and maintained by dedicated Google engineers.

This one may seem obvious, but it’s important to remember that many (not all) frameworks are made by hobbyists in the open source community. While passion and drive have forged frameworks, like Cappucino and Knockout, Angular is built and maintained by dedicated (and highly talented) Google engineers. This means you not only have a large open community to learn from, but you also have skilled, highly-available engineers tasked to help you get your Angular questions answered.

This isn’t Google’s first attempt at a JavaScript framework; they first developed their comprehensive Web Toolkit, which compiles Java down to JavaScript, and was used by the Google Wave team extensively. With the rise of HTML5, CSS3, and JavaScript, as both a front-end and back-end language, Google realized that the web was not meant to be written purely in Java.

AngularJS came about to standardize web application structure and provide a future template for how client-side apps should be developed.

Version 1.0 was released just under 6 months ago (as of December, 2012) and is being used by a host of applications, ranging from hobby to commercial products. Adoption of AngularJS as a viable framework for client-side development is quickly becoming known to the entire web development community.

Because AngularJS is built by Google, you can be sure that you’re dealing with efficient and reliable code that will scale with your project. If you’re looking for a framework with a solid foundation, Angular is your choice!


2 – It’s Comprehensive

If you’re familiar with projects, like QUnit, Mocha or Jasmine, then you’ll have no trouble learning Angular’s unit-testing API.

Angular, similar to Backbone or JavaScriptMVC, is a complete solution for rapid front-end development. No other plugins or frameworks are necessary to build a data-driven web application. Here’s an overview of Angular’s stand-out features:

  • REST Easy. RESTful actions are quickly becoming the standard for communicating from the server to the client. In one line of JavaScript, you can quickly talk to the server and get the data you need to interact with your web pages. AngularJS turns this into a simple JavaScript object, as Models, following the MVVM (Model View View-Model) pattern.
  • MVVM to the Rescue! Models talk to ViewModel objects (through something called the $scope object), which listen for changes to the Models. These can then be delivered and rendered by the Views, which is the HTML that expresses your code. Views can be routed using the $routeProvider object, so you can deep-link and organize your Views and Controllers, turning them into navigable URLs. AngularJS also provides stateless controllers, which initialize and control the $scope object.
  • Data Binding and Dependency Injection. Everything in the MVVM pattern is communicated automatically across the UI whenever anything changes. This eliminates the need for wrappers, getters/setters or class declarations. AngularJS handles all of this, so you can express your data as simply as with JavaScript primitives, like arrays, or as complex as you wish, through custom types. Since everything happens automatically, you can ask for your dependencies as parameters in AngularJS service functions, rather than one giant main() call to execute your code.
  • Extends HTML. Most websites built today are a giant series of <div> tags with little semantic clarity. You need to create extensive and exhaustive CSS classes to express the intention of each object in the DOM. With Angular, you can operate your HTML like XML, giving you endless possibilities for tags and attributes. Angular accomplishes this, via its HTML compiler and the use of directives to trigger behaviors based on the newly-created syntax you write.
  • Makes HTML your Template. If you’re used to Mustache or Hogan.js, then you can quckly grasp the bracket syntax of Angular’s templating engine, because it’s just HTML. Angular traverses the DOM for these templates, which house the directives mentioned above. The templates are then passed to the AngularJS compiler as DOM elements, which can be extended, executed or reused. This is key, because, now, you have raw DOM components, rather than strings, allowing for direct manipulation and extension of the DOM tree.
  • Enterprise-level Testing. As stated above, AngularJS requires no additional frameworks or plugins, including testing. If you’re familiar with projects, like QUnit, Mocha or Jasmine, then you’ll have no trouble learning Angular’s unit-testing API and Scenario Runner, which guides you through executing your tests in as close to the actual state of your production application as possible.

These are the fundamental principles that guide AngularJS to creating an efficient, performance-driven, and maintainable front-end codebase. As long as you have a source for storing data, AngularJS can do all of the heavy lifting on the client, while providing a rich, fast experience for the end user.


3 – Get Started in Minutes

Getting started with AngularJS is incredibly easy. With a few attributes added to your HTML, you can have a simple Angular app up in under 5 minutes!

  1. Add the ng-app directive to the <html> tag so Angular knows to run on the page:
    <html lang="en" ng-app>
  2. Add the Angular <script> tag to the end of your <head> tag:
    <head>...meta and stylesheet tags... <script src="lib/angular/angular.js"></script>
  3. Add regular HTML. AngularJS directives are accessed through HTML attributes, while expressions are evaluated with double-bracket notation:
    <body ng-controller="ActivitiesListCtrl">  <h1>Today's activities</h1>  <ul>   <li ng-repeat="activity in activities">     {{activity.name}}   </li>  </ul></body></html>

The ng-controller directive sets up a namespace, where we can place our Angular JavaScript to manipulate the data and evaluate the expressions in our HTML. ng-repeat is an Angular repeater object, which instructs Angular to keep creating list elements as long as we have activities to display, and use this <li> element as a template for how we want all of them to look.

  1. When you want to grab something from Angular, fetch your data with a JavaScript file containing a function whose name corresponds to the controller you’ve outlined in your HTML:
function ActivitiesListCtrl($scope) {  $scope.activities = [    { "name": "Wake up" },    { "name": "Brush teeth" },    { "name": "Shower" },    { "name": "Have breakfast" },    { "name": "Go to work" },    { "name": "Write a Nettuts article" },    { "name": "Go to the gym" },    { "name": "Meet friends" },    { "name": "Go to bed" }  ]; }

As mentioned previously, we’re creating a function with the same name as the ng-controller directive, so our page can find the corresponding Angular function to initialize and execute our code with the data we wish to grab. We pass in the $scope parameter in order to access the template’s activities list that we created in our HTML view. We then provide a basic set of activities with the key, name, corresponding to the activity‘s property name that we listed in our double-bracket notation, and a value, which is a string representing the activities that we want to accomplish today.

  1. While this application is complete, it’s not overly practical. Most web applications house lots of data stored on remote servers. If you’ve got your data stored on a server somewhere, we can easily replace the array with a call from Angular’s AJAX API:
function ActivitiesListCtrl($scope) {  $http.get('activities/list.json').success(function (data) {    $scope.activities = data;  }}

We’ve simply replaced the native JavaScript array object of hashes with a specialized HTTP GET function, provided by the Angular API. We pass in the name of the file that we watch to fetch from the server (in this case, a JSON file of activities) and we are returned a promise from Angular, much in the same way that the promise pattern works in jQuery.

Learn more about promises in jQuery here on Nettuts+.

This promise can then execute our success function when the data has been returned, and all we have to do is bind the return data to our activities, which as previously stated, was provided by dependency injection, via the $scope parameter.

A static to-do list is nice, but the real power stems from how easily we can manipulate the page without having to set up a bunch of JavaScript functions to listen and respond to user interactions. Imagine that we need to sort our activities list alphabetically. Well, we simply add a drop down selector to allow the user to sort the list:

<h3>Sort:</h3><select>   <option value="name">Alphabetically</option> </select>

Add the model directive to the drop down:

<select ng-model="sortModel">

Finally, we modify the <li> tag to recognize sortModel as a way to order our list:

<li ng-repeat="activity in activities | orderBy: sortModel">

All of the heavy lifting is intelligently done by AngularJS.

And that’s it! The secret is the filter we’ve added to the ng-repeat directive in the list item. The orderBy filter takes an input array (our list of activities), copies it, and reorders that copy by the property outlined in the select tag. It’s no coincidence that the value attribute of the option tag is name, the same value that is provided by our JSON file as the property of an activity. This is what allows AngularJS to automagically turn our HTML option value into a powerful keyword for sorting our activities template.

Here’s a demo of what we’ve just created in only a few lines of HTML and JS. Notice how we aren’t listening for user interaction events. Our code isn’t riddled with callbacks and event handlers for dealing with objects we’ve clicked, selected, touched or enabled. All of the heavy lifting is intelligently done by AngularJS to find the controller function, create the dependency between the template and the controller, and fetch the data to render it on the screen.

AngularJS provides a full and robust tutorial, which creates a very similar web app and expands it even more – all in a matter of minutes!


Conclusion

AngularJS is quickly becoming the dominant JavaScript framework for professional web development.

AngularJS is quickly becoming the dominant JavaScript framework for professional web development.

  • We’ve reviewed how Google came to develop this framework as a way to make the most of HTML.
  • We’ve examined the basic core features and functionality that make Angular such a pleasure to work with.
  • Finally, we’ve developed a dynamic, fully-functional demo in a matter of minutes to demonstrate the true power of how easy it is to go from nothing, to a full application.

If you’re looking for a robust, well-maintained framework for any sized project, I strongly recommend that you take a look at AngularJS. It can be downloaded for free at AngularJS.org, which also contains a wealth of information, including the full API documentation, as well as numerous examples and tutorials that cover every facet of front-end web development. Good luck!


Original Link: http://feedproxy.google.com/~r/nettuts/~3/d0Q1TKZJNk8/

Share this article:    Share on Facebook
View Full Article

TutsPlus - Code

Tuts+ is a site aimed at web developers and designers offering tutorials and articles on technologies, skills and techniques to improve how you design and build websites.

More About this Source Visit TutsPlus - Code