本期更新,博主將給大家分享一些AngularJs常用的一些屬性和方法,AngularJS 是由 Google 的員工 Miško Hevery 從 2009 年開始著手開發。這是一個非常好的構想,該項目目前已由 Google 正式支持,有一個全職的開發團隊繼續開發和維護這個庫。AngularJS 是一個 JavaScript 框架。它是一個以 JavaScript 編寫的庫。因此,有一定JavaScript基礎的朋友會更容易理解,其中的一些用法也可參照Javascript的使用方法。
一、AngularJS入門之指令與表達式
AngularJS 通過 指令 擴展了 HTML,且通過 表達式 綁定數據到 HTML。
【AngularJS常用指令】
1、ng-app:聲明Angular所管轄的區域。一般寫在body或html上,原則上一個頁面只有一個;
<body ng-app=""></body>
2、ng-model:把元素值(比如輸入域的值)綁定到應用程序的變量中。
<input type="text" ng-model="name"/>
3、ng-bind:把應用程序變量中的數據綁定到 HTML視圖中??捎帽磉_式{{ }}替代;
<div ng-bind="name"></div>
<div>{{name}}</div>
4、ng-init:初始化 AngularJS應用程序中的變量。
<body ng-app="" ng-init="name=123">
5、表達式: {{}} 綁定表達式,可以包含文字、運算符和變量。但表達式在網頁加載瞬間會看到{{}},所以可以用ng-bind=""替代
{{ 5 +""+ 5 + ',Angular'}}
【基本概念】
1、指令:AngularJS中,通過擴展HTML的屬性提供功能。所以,ng-開頭的新屬性,被我們成為指令
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>AngularJS入門</title>
</head>
<body ng-app="" ng-init="name=123">
<input type="text" id="input" ng-model="name"/>
<div id="div" ng-bind="name+',Angular'">{{name}}</div>
<input type="text" id="input2" ng-model="name"/>
<p>我的第一個表達式: {{ 5 +""+ 5 + ',Angular'}}</p>
</body>
<script src="libs/jquery-3.1.1.js"></script>
<script src="libs/angular.js"></script>
<script type="text/javascript">
// var input1 = document.getElementById("input");
// var div = document.getElementById("div");
//
// input1.onkeyup = function(){
// div.innerHTML = input1.value;
// }
// $("#input").keyup(function(){
// $("#div").html($("input").val());
// });
</script>
</html>
二、AngularJS中的MVC與作用域
[MVC三層架構]
1、Model(模型):應用程序中用于處理數據的部分。(保存或修改數據到數據庫、變量等)。AngularJS中的Model特指的是:數據
View(視圖):用戶看到的用于顯示數據的頁面;
Controller(控制器):應用程序中處理用戶交互的部分。負責從視圖讀取數據,控制用戶輸入,并向模型發送數據。
2、工作原理: 用戶從視圖層發出請求,controller接收到請求后轉發給對應的model處理,model處理完成后返回結果給controller,并在view層反饋給用戶。
主要用于CRUD類應用,不適合游戲開發和DOM操作
創建一個Angular模塊,即ng-app所綁定的部分,需傳遞兩個參數:
① 模塊名稱,即ng-app所需要綁定的名稱。ng-app="myApp"
② 數組:需要注入的模塊名稱,不需要可為空。
var app = angular.module("myApp",[]);
在Angular模塊上,創建一個控制器Controller,需要傳遞兩個參數
① Controller名稱,即ng-controller需綁定的名稱。ng-controller="myCtrl"
② Controller的構造函數:構造函數可以傳入多個參數,包括$scope/$rootScope以及各種系統內置對象;
[AngularJS中的作用域]
① $scope:局部作用域,聲明在$scope上的屬性和方法,只能在當前Controller中使用;
② $rootScope:根作用域,聲明在$rootScope上的屬性和方法,可以在ng-app所包含的任何區域使用(無論是否同一Controller,或是否在Controller包含范圍中)。
>>> 若沒有使用$scope聲明變量,而直接在html中使用ng-model綁定的變量作用域為:
1.如果ng-model在某個ng-controller中,則此變量會默認綁定到當前Controller的$scope上;
2.如果ng-model沒有在任何一個ng-controller鐘,則此變量將綁定在$rootScope上;
app.controller("myCtrl",function($scope,$rootScope){
//$scope.name = "name1";
$rootScope.age = 14;
$scope.classes = {
name:"H51701",
num:"33"
};
});
app.controller("myCtrl1",function(){
});
三、Angular過濾器
AngularJS中,過濾器可以使用一個管道字符(|)添加到表達式和指令中。
>>> 系統內置過濾器:
currency 格式化數字為貨幣格式。
filter 從數組項中選擇一個子集。
lowercase 格式化字符串為小寫。
orderBy 根據某個表達式排列數組。
uppercase 格式化字符串為大寫。
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<link rel="stylesheet" href="css/bootstrap.css" rel="external nofollow" />
</head>
<body ng-app="app" ng-controller="ctrl">
<p>{{"aBcDeF"|uppercase}}</p>
<p>{{"aBcDeF"|lowercase}}</p>
<p>{{123456|currency}}</p>
<form class="form-horizontal">
</form>
<div class="form-group">
<label>請輸入篩選信息:</label>
<input type="text" ng-model="search" />
</div>
<table class="table table-bordered">
<thead>
<tr>
<th>姓名</th>
<th>年齡</th>
<th>成績</th>
</tr>
</thead>
<tr ng-repeat="item in classes| filter:search | orderBy:'score'">
<td>{{item.name}}</td>
<td>{{item.age}}</td>
<td>{{item.score}}</td>
</tr>
</table>
<p>{{"123456"|reverse}}</p>
</body>
<script src="libs/angular.js"></script>
<script>
angular.module("app",[])
.controller("ctrl",function($scope){
$scope.classes = [
{name:"張二",age:"12",score:"88"},
{name:"張三",age:"12",score:"88"},
{name:"李四",age:"15",score:"78"},
{name:"李五",age:"15",score:"78"},
{name:"王大麻子",age:"18",score:"98"},
{name:"王二麻子",age:"18",score:"98"}
];
})
/*
* 自定義過濾器
*/
.filter('reverse', function() {
return function(text) {
if(!angular.isString(text)){
return "您輸入的內容不是字符串";
}else{
return text.split("").reverse().join("");
}
}
})
</script>
</html>
四、Angular服務Service
【服務Service】
1、內置服務:
>>>使用內置服務必須在controlller中通過函數的參數注入進來?。。?!
$location :返回當前頁面的 URL 地址;
$http: 服務器請求數據,類似于AJax;
$timeout :對應了 JS window.setTimeout 函數。
$interval :對應了 JS window.setInterval 函數。
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body ng-app="app" ng-controller="ctrl">
<pre >{{local}}</pre>
<p ng-bind="myHeader"></p>
<p ng-bind="num"></p>
<p >{{gongneng}}</p>
<p>將255轉為16進制:{{hexafy}}</p>
<p>{{123|filt}}</p>
<p>{{123|filt1}}</p>
</body>
<script type="text/javascript" src="libs/angular.js" ></script>
<script type="text/javascript">
angular.module("app",[])
.controller("ctrl",function ($scope,$location,$timeout,$interval,$hexafy) {
$scope.local=$location.absUrl();
$scope.myHeader = "Hello World!";
$timeout(function () {
$scope.myHeader = "How are you today?";
}, 2000);
$scope.num=0;
$interval(function () {
$scope.num++;
},1000);
$scope.gongneng=$hexafy.$$gongneng;
$scope.hexafy=$hexafy.myFunc(255);
})
/*自定義服務*/
.service('$hexafy', function() {
this.$$gongneng = "將轉入的數字,轉為16進制";
this.myFunc = function (x) {
return x.toString(16);
}
})
.filter("filt",function(){
return function(x){
return x.toString(16);
}
})
/*在過濾器中,調用自定義服務*/
.filter("filt1",function($hexafy){
return function(x){
return $hexafy.myFunc(x);
}
})
</script>
</html>
【自定義服務factory】
factory 是一個函數用于返回值,通常我們使用 factory 函數來計算或返回值。(factory使用上,與service差距不大)
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<link rel="stylesheet" href="libs/bootstrap.css" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" />
</head>
<body ng-app="app" ng-controller="ctrl">
<p>
[功能]<br/>
{{gongneng}}
</p>
<p>
255轉成16進制為:{{num}}
</p>
</body>
<script src="libs/angular.js"></script>
<script>
angular.module("app",[])
.config()
.controller("ctrl",function($scope,hexafy){
$scope.gongneng = hexafy.gongneng;
$scope.num = hexafy.myFunc(255);
})
.factory('hexafy',function(){
var obj = {
gongneng : "將轉入的數字,轉為16進制",
myFunc:function(x){
return x.toString(16);
}
};
return obj;
})
</script>
</html>
【自定義服務provide】
1、在AngularJS中,Service,factory都是基于provider實現的。
2、在provider中,通過$get()方法提供了factory的寫法,用于返回 value/service/factory。;
3、provider是三種自定義服務中,唯一可以寫進config配置階段的一種。
如果服務,必須要在配置階段執行,那么必須使用provider。否則,一般使用Service或factory
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<link rel="stylesheet" href="libs/bootstrap.css" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" />
</head>
<body ng-app="app" ng-controller="ctrl">
<p>
[功能]<br/>
{{gongneng}}
</p>
<p>
255轉成16進制為:{{num}}
</p>
</body>
<script src="libs/angular.js"></script>
<script>
angular.module("app",[])
/*在配置階段聲明procide服務!*/
.controller("ctrl",function($scope,hexafy){
$scope.gongneng = hexafy.gongneng;
$scope.num = hexafy.myFunc(255);
})
/*定義一個provider服務*/
.provider('hexafy',function(){
// this.gongneng = "將轉入的數字,轉為16進制";
this.$get = function(){
var obj = {
gongneng : "將轉入的數字,轉為16進制",
myFunc : function(x){
return x.toString(16);
}
}
return obj;
}
})
</script>
</html>
五、Angular中的$http
$http 是 AngularJS 中的一個核心服務,用于讀取遠程服務器的數據。
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<link rel="stylesheet" href="libs/bootstrap.css" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" />
</head>
<body ng-app="app" ng-controller="ctrl">
<!--<pre>
{{data}}
</pre>-->
<div class="container" >
<div class="panel panel-primary" >
<div class="panel-heading">
<div class="panel-title" >H5-1701班級信息表</div>
</div>
<div class="panel-body">
<table class="table table-bordered">
<thead>
<tr>
<th>姓名</th>
<th>年齡</th>
<th>愛好</th>
<th>語文成績</th>
<th>數學成績</th>
<th>總分</th>
</tr>
</thead>
<tr ng-repeat="item in data | orderBy:'score.chinese + score.math'">
<td ng-bind="item.name"></td>
<td ng-bind="item.age"></td>
<td ng-bind="item.hobby">
<!--<span ng-repeat="a in item.hobby">{{a}}</span>-->
</td>
<td ng-bind="item.score.chinese"></td>
<td ng-bind="item.score.math"></td>
<td ng-bind="item.score.chinese + item.score.math"></td>
</tr>
</table>
</div>
</div>
</div>
</body>
<script src="libs/angular.js"></script>
<script>
angular.module("app",[])
.controller("ctrl",function($scope,$http){
$http.post('h61701.json',{/*傳遞的參數對象*/})
.then(function(res){
$scope.data = res.data;//data 從返回的信息中,取出需要的數據。為JSON對象(數組)
});
})
</script>
</html>
六、Angular中的select
使用數組作為數據源。
其中,x表示數組的每一項。
默認會將x直接綁定到option的value中。
而option顯示的內容,有前面的x for... 決定
<select ng-model="name" ng-options="x.site for x in sites"> </select>
使用對象作為數據源.
其中,(x,y)表示鍵值對,x為鍵,y為值。
默認會將值y綁定到option的value中.
而option顯示的內容,有前面的x for... 決定
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<link rel="stylesheet" href="libs/bootstrap.css" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" />
<style type="text/css">
*{
margin: 10px;
}
</style>
</head>
<body ng-app="app" ng-controller="ctrl">
<select ng-model="name" ng-options="x for (x, y) in sites">
</select>
<div class="alert alert-info" >
您選擇的是:{{name}}
</div>
<table class="table table-bordered">
<tr>
<th>序號</th>
<th>姓名</th>
</tr>
<tr ng-repeat="item in options">
<td>{{$index +1}}</td><!--$index 表示遍歷的索引,從0開始-->
<td>{{item}}</td>
</tr>
</table>
</body>
<script src="libs/angular.js"></script>
<script>
angular.module("app",[])
.controller("ctrl",function($scope){
$scope.options = ["張三","李四","王二麻子","杰小瑞"];
$scope.sites = {
site01 : "Google",
site02 : "Runoob",
site03 : "Taobao"
};
})
</script>
</html>
七、Angular中的DOM與事件
ng-disabled="true/false" 當傳入true時,控件禁用。傳入false是,啟用;
ng-show 默認隱藏 傳入true時顯示;
ng-hide 默認顯示 傳入true是隱藏;
ng-click定義了AngularJS中的點擊事件。
只能觸發綁定在Angular作用域中的屬性與方法。
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<link rel="stylesheet" href="libs/bootstrap.css" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" />
<style type="text/css">
*{
margin: 10px;
}
</style>
</head>
<body ng-app="app" ng-controller="ctrl">
<input type="checkbox" ng-model="mySwitch">是否同意我是帥哥!
</label>
<button ng-disabled="!mySwitch" class="btn btn-primary">點我!</button>
<p></p>
<label>
<input type="checkbox" ng-model="mySwitch2">是否顯示?
</label>
<button ng-show="mySwitch2" class="btn btn-primary">點我!</button>
<p></p>
<label>
<input type="checkbox" ng-model="mySwitch3">是否隱藏?
</label>
<button ng-hide="mySwitch3" class="btn btn-primary">點我!</button>
<p></p>
<button ng-click="count = count + 1">點我!</button>
<p>{{ count }}</p>
<button ng-click="func()">說一下感想吧!</button>
</body>
<script src="libs/angular.js"></script>
<script>
angular.module("app",[])
.controller("ctrl",function($scope,$rootScope){
$scope.count = 10;
$scope.func = function(){
alert("我彈了一個窗!");
}
})
</script>
</html>
八、Angular表單和輸入驗證
[AngularJS中的表單驗證]
1、表單中,常用的驗證操作:
$dirty 表單有填寫記錄
$valid 字段內容合法的
$invalid 字段內容是非法的
$pristine 表單沒有填寫記錄
$error 表單驗證不通過的錯誤信息
2、驗證時,需給表單,及需要驗證的input,設置name屬性:
給form及input設置name后,會將form表單信息,默認綁定到$scope作用域中。故,可以使用 formName.inputName.$驗證操作 得到驗證結果:
例如:formName.inputName.$dirty = "true" 表單被填寫過
formName.inputName.$invalid = "true" 表單輸入不合法
formName.inputName.$error.required = "true" 表單必填但未填
$error支持的驗證有:required/minlength/maxlength/pattern/email/number/date/url等。。。
3、為避免沖突,例如使用type="email"時,H5也會進行驗證操作。如果只想使用AngularJS驗證,可以使用<form novalidate></form>屬性,禁用H5自帶驗證功能;
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<link rel="stylesheet" href="libs/bootstrap.css" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" />
<style type="text/css">
.row{
margin-bottom: 10px;
}
.row .col-xs-5{
text-align: center;
}
.suc{
border-color: #3c763d;
-webkit-box-shadow: inset 0 1px 1px rgba(0, 0, 0, .075);
box-shadow: inset 0 1px 1px rgba(0, 0, 0, .075);
}
.suc:focus{
border-color: #2b542c;
-webkit-box-shadow: inset 0 1px 1px rgba(0, 0, 0, .075), 0 0 6px #67b168;
box-shadow: inset 0 1px 1px rgba(0, 0, 0, .075), 0 0 6px #67b168;
}
.err{
border-color: #a94442;
-webkit-box-shadow: inset 0 1px 1px rgba(0, 0, 0, .075);
box-shadow: inset 0 1px 1px rgba(0, 0, 0, .075);
}
.err:focus{
border-color: #843534;
-webkit-box-shadow: inset 0 1px 1px rgba(0, 0, 0, .075), 0 0 6px #ce8483;
box-shadow: inset 0 1px 1px rgba(0, 0, 0, .075), 0 0 6px #ce8483;
}
</style>
</head>
<body ng-app="app" ng-controller="ctrl">
<div class="container" >
<div class="panel panel-primary">
<div class="panel-heading">
<div class="panel-title" >
用戶注冊
</div>
</div>
<div class="panel-body">
<form action="" method="get" class="form-horizontal" name="myForm" novalidate>
<div class="row" >
<div class="col-xs-3">
用戶名:
</div>
<div class="col-xs-9">
<input type="text" class="form-control" ng-model="user.name" name="name" ng-minlength="4" ng-maxlength="10" required ng-class="{suc:myForm.name.$valid && myForm.name.$dirty,err:myForm.name.$invalid && myForm.name.$dirty}"/>
<p ng-show="myForm.name.$invalid && myForm.name.$dirty">
<!--當有填寫記錄且不合法時,p顯示-->
<span ng-show="myForm.name.$error.required">用戶名必須填寫?。?!</span>
<span ng-show="myForm.name.$error.minlength">用戶名最少包含4個字符?。?!</span>
<span ng-show="myForm.name.$error.maxlength">用戶名最多包含10個字符?。?!</span>
</p>
</div>
</div>
<div class="row">
<div class="col-xs-3">
郵箱:
</div>
<div class="col-xs-9">
<input type="email" class="form-control" ng-model="user.mail" name="mail" required ng-class="{suc:myForm.mail.$valid && myForm.mail.$dirty,err:myForm.mail.$invalid && myForm.mail.$dirty}"/>
<p ng-show="myForm.mail.$invalid && myForm.mail.$dirty">
<!--當有填寫記錄且不合法時,p顯示-->
<span ng-show="myForm.mail.$error.required">郵箱必須填寫?。?!</span>
<span ng-show="myForm.mail.$error.email">郵箱格式不合法?。?!</span>
</p>
</div>
</div>
<div class="row">
<div class="col-xs-3">
密碼:
</div>
<div class="col-xs-9">
<input type="password" class="form-control" ng-model="user.pwd" name="pwd" pattern="^\w{6,18}$" required ng-class="{suc:myForm.pwd.$valid && myForm.pwd.$dirty,err:myForm.pwd.$invalid && myForm.pwd.$dirty}"/>
<p ng-show="myForm.pwd.$invalid && myForm.pwd.$dirty">
<!--當有填寫記錄且不合法時,p顯示-->
<span ng-show="myForm.pwd.$error.pattern">密碼應為6-18位,且只能為字母、數字、下劃線</span>
</p>
</div>
</div>
<div class="row">
<div class="col-xs-3">
確認密碼:
</div>
<div class="col-xs-9">
<input type="password" class="form-control" ng-model="rePwd" name="rePwd" required ng-class="{suc:myForm.rePwd.$dirty&&rePwd==user.pwd,err:myForm.rePwd.$dirty&&rePwd!=user.pwd}"/>
<p ng-show="myForm.rePwd.$dirty && rePwd!=user.pwd">
<!--當有填寫記錄且不合法時,p顯示-->
兩次密碼輸入不一致?。?!
</p>
</div>
</div>
<div class="row">
<div class="col-xs-5">
<input type="submit" value="注冊" class="btn btn-success" ng-disabled="myForm.$invalid || rePwd!=user.pwd" />
</div>
<div class="col-xs-5">
<input type="button" value="重置" class="btn btn-warning" ng-click="resets()" />
</div>
</div>
</form>
</div>
</div>
<pre>
{{user.pwd}}
</pre>
</div>
</body>
<script src="libs/angular.js"></script>
<script>
$scope.resets = function(){
$scope.user = angular.copy($scope.initUser);
}
$scope.resets();
})
</script>
</html>
以上就是本文的全部內容,希望本文的內容對大家的學習或者工作能帶來一定的幫助,同時也希望多多支持億速云!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。