AngularJS tutorial - Lesson 2
2020-12-13 04:05
标签:des style blog class code c Directives are one of
the most powerful of AngularJS. They allow us to extend HTML to
answer the needs of web applications. Directives could specify how your page
should be sructured for the data available a given scope. First let’s have a look at how directives are working. Example: The actual effect of display In this example, The result exactly what we were expecting, a new div has
been created for each of my entity with their name as title and threir
description in a paragraph. Note: Angular determine if an element should be displayed or not with the directive
“ngShow“. This
directive used an expression which returns a boolean to determine if the element
should be displayed or not. Example: The actual effect of display AS we can see in the result, only females are displayed. If you inspect the
dom, we would see that the other elements have been computed but their are just
hidden(display = none). AngularJS also
contains more complex directive like “ngSwitch“. For example: Using these directives, we have ability to define the basic structure of our
web application very easily. You can download the source code of examples. Written
By smaltdd@gmail.com AngularJS tutorial - Lesson 2,搜素材,soscw.com AngularJS tutorial - Lesson 2 标签:des style blog class code c 原文地址:http://www.cnblogs.com/smaltdd/p/3730840.htmlAngularJS
Directives
AngularJS has several directives which
could build our basic application. The first directive “ngRepeat”
is used mostly. It create a new set of elements in the dom for each element in a
collection.div>
div data-ng-repeat="user in users">
h3>{{user.name}}h3>
p>{{user.description}}p>
div>
div>
$scope.users = [
{
name: ‘Tyrion‘,
description: ‘yongest son of lord Tywin‘,
gender: ‘male‘,
house: ‘Lannister‘
},
{
name: ‘Daenerys‘,
description: ‘Only daughter of Aerys II‘,
gender: ‘female‘,
house: ‘Targaryen‘
},
{
name: ‘Arya‘,
description: ‘Younger daughter of Eddard‘,
gender: ‘female‘,
house: ‘Stark‘
},
{
name: ‘Jon‘,
description: ‘Bastard Son of lord Eddard‘,
gender: ‘male‘,
house: ‘Stark‘
}
];
}]);
Tyrion
yongest son of lordTywin Daenerys Only daughter of Aerys II
Arya
Younger daughter of EddardJon BastardSon of lord Eddard
For those who are wondering why I have
prefixed “ng-repeat” by “data-“, Please have a look here
div>
div data-ng-repeat="user in users" data-ng-show="user.gender == ‘female‘">
h3>{{user.name}}h3>
p>{{user.description}}p>
div>
div>
DaenerysOnly daughter of Aerys II
AryaYounger daughter of Eddard
div ng-controller="UserCtrl">
div data-ng-repeat="user in users"
data-ng-show="user.gender == ‘female‘"
data-ng-switch="user.house">
h3>{{user.name}}h3>
p>{{user.description}}p>
Sigil:
img src="images/targaryen.png" data-ng-switch-when="Targaryen">
img src="images/stark.png" data-ng-switch-when="Stark">
img src="images/lannister.png" data-ng-switch-when="Lannister">
div>
div>
In fact, There are many Directives in
AngularJS. I intruducted only a little. HERE list
the integrated directives.
Example
code
AngularJS tutorial
05/15/2014
上一篇:Java泛型
下一篇:Python初学——列表
文章标题:AngularJS tutorial - Lesson 2
文章链接:http://soscw.com/essay/28846.html