ng-init
=
"name='World'"
> Name
ng-model
=
"name"
/> Hello, {{ name }}!
ng-bind
=
"name"
>
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. Bind data from the model to the view on HTML controls. Bind an
to the
name
property with
ng
-
model
4.
ng-model
=
"name"
/>
GoalKicker.com – AngularJS Notes for Professionals
7
Display content from the model using double braces
{{
}}
5.
Hello, {{ name }}
Another way of binding the
name
property is using
ng
-
bind
instead of handlebars
"{{ }}"
6.
ng-bind
=
"name"
> 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:
ng-app
=
"myDemoApp"
>
.started { background: gold; }
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]);
ng-class
=
"{ 'started': hasStarted }"
ng-cloak
>
ng-controller
=
"demoController as vm"
>
GoalKicker.com – AngularJS Notes for Professionals
8