CHAPTER 1
You wish to use Angular.js on a web page.
In order to get your first Angular.js app up and running, you need to include the Angular JavaScript file via script tag and make use of the ng-app directive:
<html> |
You can check out a complete example on GitHub.
Adding the ng-app directive tells Angular to kick in its magic. The expression {{ 1 + 2 }} is evaluated by Angular and the result 3 is rendered. Note that removing ng-app will result in the browser rendering the expression as is instead of evaluating it. Play around with the expression! You can, for instance, concatenate Strings and invert or combine Boolean values.
For reasons of brevity, we will skip the boilerplate code in the following recipes.
You want user input to be used in another part of your HTML page.
Use Angular's ng-model directive to bind the text input to the expression:
Enter your name: <input type="text" ng-model="name"></input> |
You can find the complete example on GitHub.
Assigning "name" to the ng-model attribute and using the name variable in an expression will keep both in sync automatically. Typing in the text input will automatically reflect these changes in the paragraph element below.
Consider how you would implement this traditionally using jQuery:
<html> |
On document ready, we bind to the keypress event in the text input and replace the text in the paragraph in the callback function. Using jQuery, you need to deal with document ready callbacks, element selection, event binding, and the context of this. Quite a lot of concepts to swallow and lines of code to maintain!
You wish to hide an HTML element on button click.
Use the ng-hide directive in conjunction with a controller to change the visibility status on button click:
<html> |
And the controller in js/app.js:
function MyCtrl($scope) { |
You can find the complete example on GitHub.
When toggling the button, the "Hello World" paragraph will change its visibility.
Using the ng-controller directive, we bind the div element, including its children, to the context of the MyCtrl controller. The ng-click directive will call the toggle() function of our controller on button click. Note that the ng-show directive is bound to the visible scope variable and will toggle the paragraph's visibility accordingly.
The controller implementation defaults the visible attribute to true and toggles its Boolean state in the toggle function. Both the visible variable and the toggle function are defined on the $scope service, which is passed to all controller functions automatically via dependency injection.
The next chapter will go into all the details of controllers in Angular. For now, let us quickly discuss the Model-View-ViewModel (MVVM) pattern as used by Angular. In the MVVM pattern, the model is plain JavaScript, the view is the HTML template, and the ViewModel is the glue between the template and the model. The ViewModel makes Angular's two-way binding possible where changes in the model or the template are in sync automatically.
In our example, the visible attribute is the model, but it could of course be much more complex when, for example, retrieving data from a backend service. The controller is used to define the scope which represents the ViewModel. It interacts with the HTML template by binding the scope variable visible and the function toggle() to it.
When presenting data to the user, you might need to convert the data to a more user-friendly format. In our case, we want to uppercase the name value from the previous recipe in the expression.
Use the uppercase Angular filter:
Enter your name: <input type="text" ng-model="name"></input> |
You can find the complete example on GitHub.
Angular uses the | (pipe) character to combine filters with variables in expressions. When evaluating the expression, the name variable is passed to the uppercase filter. This is similar to working with the Unix bash pipe symbol where an input can be transformed by another program. Also, try the lowercase filter!
You wish to render an HTML snippet but hide it conditionally.
Create a custom directive which renders your Hello World snippet:
<body ng-app="MyApp"> |
The show attribute is our directive, with the following implementation:
var app = angular.module("MyApp", []); |
The browser will only show the "Hello World" paragraph if the checkbox is checked and will hide it otherwise.
You can find the complete example on GitHub.
Let us ignore the app module creation for now and look into it in more depth in a later chapter. In our example, it is just the means to create a directive. Note that the show String is the name of the directive which corresponds to the show HTML attribute used in the template.
The directive implementation returns a link function that defines the directive's behavior. It gets passed the scope, the HTML element, and the HTML attributes. Since we passed the visible variable as an attribute to our show directive, we can access it directly via attributes.show. But since we want to respond to visible variable changes automatically, we have to use the $watch service, which provides us with a function callback whenever the value changes. In this callback, we change the CSS display property to either blank or none to hide the HTML element.
This was just a small glimpse into what can be achieved with directives; there is a whole chapter later that is dedicated to them, which goes into much more detail.