以 JavaScript 編寫的庫,是一個 JavaScript 框架express
AngularJS 使用 表達式 把數據綁定到 HTML。數組
AngularJS 指令是擴展的 HTML 屬性,帶有前綴 ng-。app
ng-app | 初始化一個 AngularJS 應用程序(告訴 AngularJS,ng-app所在元素是 AngularJS 應用程序 的"全部者"。)
<div ng-app="" ng-init="firstName='John'"> <p>在輸入框中嘗試輸入:</p> <p>姓名:<input type="text" ng-model="firstName"></p> <p>你輸入的爲: {{ firstName }}</p> </div> |
ng-init | 定義初始值 |
ng-model | 把元素值(好比輸入域的值)綁定到應用程序。用於表單元素的,支持雙向綁定(在修改輸入域的值時, AngularJS 屬性的值也將修改:)。對普通元素無效({數據綁定:{ firstName }} 是經過 ng-model="firstName" 進行同步。)框架 模型ide ng-model 指令用於綁定應用程序數據到 HTML 控制器(input, select, textarea)的值ui <div ng-app="myApp" ng-controller="myCtrl"> 名字: <input ng-model="name"> </div> <script> var app = angular.module('myApp', []); app.controller('myCtrl', function($scope) { $scope.name = "John Doe"; }); </script> <form ng-app="" name="myForm"> Email: <input type="email" name="myAddress" ng-model="text"> <span ng-show="myForm.myAddress.$error.email">不是一個合法的郵箱地址</span> </form> <form ng-app="" name="myForm" ng-init="myText = 'test@runoob.com'"> Email: <input type="email" name="myAddress" ng-model="myText" required></p> <h1>狀態</h1> {{myForm.myAddress.$valid}} {{myForm.myAddress.$dirty}} {{myForm.myAddress.$touched}} </form>
|
ng-repeat | 對於集合中(數組中)的每一個項會 克隆一次 HTML 元素<div data-ng-app="" data-ng-init="names=['Jani1','Hege','Kai']"> <p>使用 ng-repeat 來循環數組</p> <ul> <li data-ng-repeat="x in names"> {{ x }} </li> </ul> </div> |
建立自定義指令:雙向綁定
<body ng-app="myApp"> <runoob-directive></runoob-directive> <script> var app = angular.module("myApp", []); app.directive("runoobDirective", function() { return { template : "<h1>自定義指令!</h1>" }; }); </script>
<body ng-app="myApp"> <div runoob-directive></div> <script> var app = angular.module("myApp", []); app.directive("runoobDirective", function() { return { template : "<h1>自定義指令!</h1>" }; }); </script>
<body ng-app="myApp"> <div class="runoob-directive"></div> <script> var app = angular.module("myApp", []); app.directive("runoobDirective", function() { return { restrict : "C", template : "<h1>自定義指令!</h1>" }; }); </script>
<body ng-app="myApp"> <!-- directive: runoob-directive --> <script> var app = angular.module("myApp", []); app.directive("runoobDirective", function() { return { restrict : "M", replace : true, template : "<h1>自定義指令!</h1>" }; }); </script>
restrict 默認值爲 EA
, 便可以經過元素名和屬性名來調用指令。rest