AngularJS Notes for Professionals [LIVRE]
Résumer
Section 1.1: Getting Started
Create a new HTML file and paste the following content:
<!DOCTYPE html>
<html ng-app>
<head>
<title>Hello, Angular</title>
<script src="https://code.angularjs.org/1.5.8/angular.min.js"></script>
</head>
<body ng-init="name='World'">
<label>Name</label>
<input ng-model="name" />
<span>Hello, {{ name }}!</span>
<p ng-bind="name"></p>
</body>
</html>
Live demo
When you open the file with a browser, you will see an input field followed by the text Hello, World!. Editing the
value in the input will update the text in real-time, without the need to refresh the whole page.
Explanation:
1. Load the Angular framework from a Content Delivery Network.
<script src="https://code.angularjs.org/1.5.8/angular.min.js"></script>
2. Define the HTML document as an Angular application with the ng-app directive
<html ng-app>
3. Initialize the name variable using ng-init
<body ng-init=" name = 'World' ">
Note that ng-init should be used for demonstrative and testing purposes only. When building an actual application,
controllers should initialize the data.
4. Bind data from the model to the view on HTML controls. Bind an <input> to the name property with ng-model
<input ng-model="name" />
5. Display content from the model using double braces {{ }}
<span>Hello, {{ name }}</span>
6. Another way of binding the name property is using ng-bind instead of handlebars"{{ }}"
<span ng-bind="name"></span>
The last three steps establish the two way data-binding. Changes made to the input update the model, which is
reflected in the view.
There is a difference between using handlebars and ng-bind. If you use handlebars, you might see the actual
Hello, {{name}} as the page loads before the expression is resolved (before the data is loaded) whereas if you use
ng-bind, it will only show the data when the name is resolved. As an alternative the directive ng-cloak can be used
to prevent handlebars to display before it is compiled.
Section 1.2: Showcasing all common Angular constructs
The following example shows common AngularJS constructs in one file:
<!DOCTYPE html>
<html ng-app="myDemoApp">
<head>
<style>.started { background: gold; }</style>
<script src="https://code.angularjs.org/1.5.8/angular.min.js"></script>
<script>
function MyDataService() {
return {
getWorlds: function getWorlds() {
return ["this world", "another world"];
}
};
}
function DemoController(worldsService) {
var vm = this;
vm.messages = worldsService.getWorlds().map(function(w) {
return "Hello, " + w + "!";
});
}
function startup($rootScope, $window) {
$window.alert("Hello, user! Loading worlds...");
$rootScope.hasStarted = true;
}
angular.module("myDemoApp", [/* module dependencies go here */])
.service("worldsService", [MyDataService])
.controller("demoController", ["worldsService", DemoController])
.config(function() {
console.log('configuring application');
})
.run(["$rootScope", "$window", startup]);
</script>
</head>
<body ng-class="{ 'started': hasStarted }" ng-cloak>
<div ng-controller="demoController as vm">
<ul>
<li ng-repeat="msg in vm.messages">{{ msg }}</li>
</ul>
</div>
</body>
</html>
Every line of the file is explained below:
Live Demo
1. ng-app="myDemoApp", the ngApp directive that bootstraps the application and tells angular that a DOM
element is controlled by a specific angular.module named "myDemoApp";
2. <script src="angular.min.js"> is the first step in bootstrapping the AngularJS library;
Three functions (MyDataService, DemoController, and startup) are declared, which are used (and explained)
below.
3. angular.module(...) used with an array as the second argument creates a new module. This array is used
to supply a list of module dependencies. In this example we chain calls on the result of the module(...)
function;
4. .service(...) creates an Angular Service and returns the module for chaining;
5. .controller(...) creates an Angular Controller and returns the module for chaining;
6. .config(...) Use this method to register work which needs to be performed on module loading.
7. .run(...) makes sure code is run at startup time and takes an array of items as a parameter. Use this
method to register work which should be performed when the injector is done loading all modules.
the first item is letting Angular know that the startup function requires the built-in $rootScope service
to be injected as an argument;
the second item is letting Angular know that the startup function requires the built-in $window service
to be injected as an argument;
the last item in the array, startup, is the actual function to run on startup;
8. ng-class is the ngClass directive to set a dynamic class, and in this example utilizes hasStarted on the
$rootScope dynamically
9. ng-cloak is a directive to prevent the unrendered Angular html template (e.g. "{{ msg }}") to be briefly
shown before Angular has fully loaded the application.
10. ng-controller is the directive that asks Angular to instantiate a new controller of specific name to
orchestrate that part of the DOM;
11. ng-repeat is the directive to make Angular iterate over a collection and clone a DOM template for each item;
12. {{ msg }} showcases interpolation: on-the-spot rendering of a part of the scope or controller;
Resultats du Test [AngularJS Notes for Professionals ]
Surnom | Date Test | Points Obtenu |
---|
L'AUTEUR DU TUTORIEL
RUSSELL EYENGA
Niveau d'études : L2