var objs = [{a: 1}, {a: 2}];
angular.forEach(objs, function(data, index, array) {
//data等價於array[index]
console.log(data.a + '=' + array[index].a);
});
var objs = {"a":1,"b":2}
angular.forEach(objs, function(data, index, array) {
//1 "a" {a: 1, b: 2}
console.log(data,index,array);
});
複製代碼
angular 中自認可第一個 ng-app,經過 var app =angular.module("myApp",[]);便可獲它的操做權;此時咱們須要手動的初始化第二個 ng-appjavascript
angular.bootstrap 來手動添加html
<div id="A1" ng-app="app1">
<input ng-model="name" type="text" placeholder="請輸入姓名">
<p>個人姓名: {{name}}</p>
</div>
<div id="A2" ng-app="app2">
<input ng-model="age" type="number" placeholder="請輸入年齡">
<p>個人年齡: {{age}}</p>
</div>
<script type="text/javascript"> var app1 = angular.module("app1", []); //自動加載 var app2 = angular.module("app2", []); //手動加載 angular.bootstrap(document.getElementById("A2"), ['app2']); // 手動加載2 </script>
複製代碼
<div ng-app="myApp" ng-controller="myCtrl as ctrl">
<div id="addEle"></div>
</div>
<script> var app = angular.module("myApp", []); app.controller("myCtrl", function($scope, $compile) { var vm = this; vm.msg = 'hello'; // 建立編譯函數 var compileFn = $compile('<div>{{ctrl.msg}}</div>'); // 傳入scope,獲得編譯好的dom對象(已封裝爲jqlite對象) // 也能夠用$scope.$new()建立繼承的做用域 var $dom = compileFn($scope); //返回的$dom是一個jQLite(jQuery的子集)對象 console.log($dom); // 添加到文檔中 var jqLite = angular.element(document.getElementById("addEle")); jqLite.append($dom); //$dom.remove(); //移除語句 }); </script>
複製代碼
var jqEle = $("#myDiv"); //jquery寫法
var jqLite = angular.element(document.getElementById("myDiv")); //jqlite寫法
複製代碼
經常使用的一些過濾器,也能夠本身封裝 和 補充 service 來自帶參數。java
//自定義過濾器
app.filter('addOne', function() {
return function(i) {
var result = 0;
if(!isNaN(i)) {
result = 1 + parseInt(i);
}
return result;
}
});
//自定義過濾器帶參數
app.service('addOneService', function() {
this.add = function(i) {
var result = 0;
if(!isNaN(i)) {
result = 1 + parseInt(i);
}
return result;
}
});
複製代碼
$error.required 惟一值驗證
$error.email 文本輸入內置電子郵件驗證。
$error.number 帶有數量驗證的文本輸入。也能夠有最小和最大值的附加屬性。
$error.date 帶有輸入日期文本輸入。
$error.url 帶有輸入驗證的URL文本輸入。
$error.minlength,參數範圍需從input中ng-minlength設置
$error.maxlength,參數範圍需從input中ng-maxlength設置
$error.pattern,正則表達式需從input中ng-pattern設置
$dirty 表單有填寫記錄
$pristine 表單沒有填寫記錄
$valid 字段內容合法的,如formname.$valid
$invalid 字段內容是非法的
複製代碼
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>demo-ng-switch</title>
<script type="text/javascript" src="../js/libs/angular/angular.js" ></script>
</head>
<body>
<div ng-app="myApp" ng-controller="myCtrl">
<form>
選擇一個選項:
<input type="radio" ng-model="myVar" value="dogs">Dogs
<input type="radio" ng-model="myVar" value="tuts">Tutorials
<input type="radio" ng-model="myVar" value="cars">Cars
</form>
<div ng-switch="myVar">
<div ng-switch-when="dogs">
<h1>Dogs</h1>
<p>Welcome to a world of dogs.</p>
</div>
<div ng-switch-when="tuts">
<h1>Tutorials</h1>
<p>Learn from examples.</p>
</div>
<div ng-switch-when="cars">
<h1>Cars</h1>
<p>Read about cars.</p>
</div>
</div>
</div>
<script> var app = angular.module("myApp", []); app.controller("myCtrl", function($scope) {}); </script>
</body>
</html>
複製代碼