ng-app 指令定義一個 AngularJS 應用程序僻焚。
ng-model 指令把元素值(比如輸入域的值)綁定到應用程序丈探。
ng-bind 指令把應用程序數(shù)據(jù)綁定到 HTML 視圖。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="https://cdn.static.runoob.com/libs/angular.js/1.4.6/angular.min.js"></script>
</head>
<body>
<div ng-app="">
<p>名字 : <input type="text" ng-model="name"></p>
<h1>Hello {{name}}</h1>
</div>
</body>
</html>
當網(wǎng)頁加載完畢挣饥,AngularJS 自動開啟。
ng-app 指令告訴 AngularJS,<div> 元素是 AngularJS 應用程序 的"所有者"宽涌。
ng-model 指令把輸入域的值綁定到應用程序變量 name。
ng-bind 指令把應用程序變量 name 綁定到某個段落的 innerHTML蝶棋。
AngularJS 指令
ng-init 指令初始化 AngularJS 應用程序變量卸亮。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="https://cdn.static.runoob.com/libs/angular.js/1.4.6/angular.min.js"></script>
</head>
<body>
<div ng-app="" ng-init="firstName='John'">
<p>姓名為 <span ng-bind="firstName"></span></p>
</div>
</body>
</html>
AngularJS 屬性以 ng- 開頭,但是您可以使用 data-ng- 來讓網(wǎng)頁對 HTML5 有效玩裙。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="https://cdn.static.runoob.com/libs/angular.js/1.4.6/angular.min.js"></script>
</head>
<body>
<div data-ng-app="" data-ng-init="firstName='John'">
<p>姓名為 <span data-ng-bind="firstName"></span></p>
</div>
</body>
</html>
AngularJS 表達式
AngularJS 表達式寫在雙大括號內:{{ expression }}兼贸。
AngularJS 表達式把數(shù)據(jù)綁定到 HTML,這與 ng-bind 指令有異曲同工之妙吃溅。
AngularJS 將在表達式書寫的位置"輸出"數(shù)據(jù)溶诞。
AngularJS 表達式 很像 JavaScript 表達式:它們可以包含文字、運算符和變量决侈。
實例 {{ 5 + 5 }} 或 {{ firstName + " " + lastName }}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="https://cdn.static.runoob.com/libs/angular.js/1.4.6/angular.min.js"></script>
</head>
<body>
<div ng-app="">
<p>我的第一個表達式: {{ 5 + 5 }}</p>
</div>
</body>
</html>
AngularJS 應用
AngularJS 模塊(Module) 定義了 AngularJS 應用螺垢。
AngularJS 控制器(Controller) 用于控制 AngularJS 應用。
ng-app指令定義了應用, ng-controller 定義了控制器赖歌。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="https://cdn.static.runoob.com/libs/angular.js/1.4.6/angular.min.js"></script>
</head>
<body>
<p>嘗試修改以下表單枉圃。</p>
<div ng-app="myApp" ng-controller="myCtrl">
名: <input type="text" ng-model="firstName"><br>
姓: <input type="text" ng-model="lastName"><br>
<br>
姓名: {{firstName + " " + lastName}}
</div>
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.firstName= "John";
$scope.lastName= "Doe";
});
</script>
AngularJS 模塊定義應用:
AngularJS 模塊
var app = angular.module('myApp', []);
AngularJS 控制器控制應用:
AngularJS 控制器
app.controller('myCtrl', function($scope) {
$scope.firstName= "John";
$scope.lastName= "Doe";
});