AngularJS Directives
All directive:
https://docs.angularjs.org/api
AngularJS directives are extended HTML attributes with the prefix
ng-.
The
ng-app directive initializes an AngularJS application.
The
ng-init directive initializes application data.
The
ng-model directive binds the value of HTML controls (input, select, textarea) to application data.
1<!DOCTYPE html>
2<html lang="en">
3<head>
4 <meta charset="UTF-8">
5 <meta name="viewport" content="width=device-width, initial-scale=1.0">
6 <meta http-equiv="X-UA-Compatible" content="ie=edge">
7 <title>Ví dụ đầu tiên AngularJS</title>
8
9 <!-- Include script angularJS -->
10 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
11</head>
12<body>
13 <div ng-app="" ng-init="quantity=2;price=2500">
14 <p>Tên khách hàng: <input type="text" ng-model="name"></p>
15 <p ng-bind="name"></p>
16
17 Số lượng: <input type="number" ng-model="quantity"><br />
18 Giá tiền: <input type="number" ng-model="price"><br />
19 Tổng cộng: {{ quantity * price }}
20 </div>
21</body>
22</html>
Repeating HTML Elements
The
ng-repeat directive repeats an HTML element:
1<!DOCTYPE html>
2<html lang="en">
3<head>
4 <meta charset="UTF-8">
5 <meta name="viewport" content="width=device-width, initial-scale=1.0">
6 <meta http-equiv="X-UA-Compatible" content="ie=edge">
7 <title>Ví dụ đầu tiên AngularJS</title>
8
9 <!-- Include script angularJS -->
10 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
11</head>
12<body>
13 <div ng-app="" ng-init="danhsachsanpham=[
14 {tensanpham:'hoa lan',loai:'hoa tiệc cưới', gia: 5000},
15 {tensanpham:'hoa hồng',loai:'hoa tình yêu', gia: 2000},
16 {tensanpham:'hoa tươi',loai:'hoa văn phòng', gia: 3000}
17 ]">
18
19 <ul>
20 <li ng-repeat="x in danhsachsanpham">
21 {{ x.tensanpham + ', ' + x.loai + ': ' + x.gia }}
22 </li>
23 </ul>
24
25 </div>
26</body>
27</html>