一、ng-app=" " 定义angularJS的使用范围;php
二、ng-init="变量=值;变量='值'" 初始化变量的值,有多个变量时,中间用分号隔开;css
三、ng-model="变量" 定义变量名;html
四、ng-bind="变量" 绑定变量名,获取该变量的数据。这里的变量就是第3条的变量名。可是通常都用双重花括号来获取变量的值,好比:{{变量}}。angularjs
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script src="https://cdn.staticfile.org/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>
ng-init 指令初始化 AngularJS 应用程序变量。服务器
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script src="https://cdn.staticfile.org/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 模块(Module) 定义了 AngularJS 应用。app
AngularJS 控制器(Controller) 用于控制 AngularJS 应用。spa
ng-app指令指明了应用, ng-controller 指明了控制器。code
<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>
ng-repeat 指令会重复一个 HTML 元素cdn
<div ng-app="" ng-init="names=[ {name:'Jani',country:'Norway'}, {name:'Hege',country:'Sweden'}, {name:'Kai',country:'Denmark'}]"> <p>循环对象:</p> <ul> <li ng-repeat="x in names"> {{ x.name + ', ' + x.country }} </li> </ul> </div>
Scope(做用域) 是应用在 HTML (视图) 和 JavaScript (控制器)之间的纽带。htm
Scope 是一个对象,有可用的方法和属性。
Scope 可应用在视图和控制器上。
<div ng-app="myApp" ng-controller="myCtrl"> <h1>{{lastname}} 家族成员:</h1> <ul> <li ng-repeat="x in names">{{x}} {{lastname}}</li> </ul> </div> <script> var app = angular.module('myApp', []); app.controller('myCtrl', function($scope, $rootScope) { $scope.names = ["Emil", "Tobias", "Linus"]; $rootScope.lastname = "Refsnes"; }); </script>
$http 是 AngularJS 应用中最经常使用的服务。 服务向服务器发送请求,应用响应服务器传送过来的数据。
<div ng-app="myApp" ng-controller="siteCtrl"> <ul> <li ng-repeat="x in names"> {{ x.Name + ', ' + x.Country }} </li> </ul> </div> <script> var app = angular.module('myApp', []); app.controller('siteCtrl', function($scope, $http) { $http.get("http://www.runoob.com/try/angularjs/data/sites.php") .then(function (response) {$scope.names = response.data.sites;}); }); </script>
使用 angular 显示表格是很是简单的:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script src="https://cdn.bootcss.com/angular.js/1.6.3/angular.min.js"></script> </head> <body> <div ng-app="myApp" ng-controller="customersCtrl"> <table> <tr ng-repeat="x in names"> <td>{{ x.Name }}</td> <td>{{ x.Country }}</td> </tr> </table> </div> <script> var app = angular.module('myApp', []); app.controller('customersCtrl', function($scope, $http) { $http.get("/try/angularjs/data/Customers_JSON.php") .then(function (result) { $scope.names = result.data.records; }); }); </script>
ng-show 指令可用于设置应用中的一部分是否可见 。
ng-show="false" 能够设置 HTML 元素 不可见。
ng-show="true" 能够以设置 HTML 元素可见。
如下实例使用了 ng-show 指令:
<div ng-app="myApp" ng-controller="personCtrl"> <button ng-click="toggle()">隐藏/显示</button> <p ng-show="myVar"> 名: <input type="text" ng-model="firstName"><br> 姓: <input type="text" ng-model="lastName"><br> <br> 姓名: {{firstName + " " + lastName}} </p> </div> <script> var app = angular.module('myApp', []); app.controller('personCtrl', function($scope) { $scope.firstName = "John", $scope.lastName = "Doe" $scope.myVar = true; $scope.toggle = function() { $scope.myVar = !$scope.myVar; } }); </script>