new Promise((resolve,reject)=>{
$.ajax({
url:'',
success(r){
resolve(r)
},
error(r){
reject(r)
}
})
}).then((r)=>{
console.log(r)
},(r)=>{
console.log(r)
})
Promise.all([
$.ajax('a.txt'),
$.ajax('b.txt'),
$.ajax('c.txt')
]).then((arr)=>{
console.log(arr)
},(r)=>{
console.log(r)
})
Promise.race([
$.ajax('a.txt'),
$.ajax('b.txt'),
$.ajax('c.txt')
]).then((r)=>{
console.log(r)
},(r)=>{
console.log(r)
})
Promise.all([
$http('a.txt'),
$http('b.txt'),
$http('c.txt')
]).then((arr)=>{
console.log(arr)
},(r)=>{
console.log(r)
})
$q.all([
$http('a.txt'),
$http('b.txt'),
$http('c.txt')
]).then((arr)=>{
console.log(arr)
},(r)=>{
console.log(r)
})
$interval
$apply
$.get
.post
.getJson
=========================================================
一械姻、模塊化
Angular中的模塊化 —— 比較弱
let mod = angular.module('mod',[]);
例1:
1) 在一個新的JS文件里定義了一個模塊
let mod1 = angular.module('mod1',[]);
mod1.controller('mod1_ctr1',($scope)=>{
$scope.a = 200;
});
2)在我的html文件中移袍,自身的模塊依賴于以上定義的模塊: mod1
3)在html文件中屉凯,可以同時使用自己的controlloer和模塊中的controller:mod1_ctr1
二母赵、依賴注入
把決定權交給消費者。
函數(shù)參數(shù):由調用者來決定 —— 被動的地位
function show(a,b){
alert(arguments.length);
}
Angular:函數(shù)的拯救者
let mod = .....;
mod.controller('ctr1',($scope,$q)=>{
$scope.a=12;
});
想用誰授药,就把誰注入到函數(shù)士嚎。
三呜魄、過濾器
系統(tǒng)的過濾器: date currency
time|date:'yyyy-MM-dd'
price|currency —— $
|currency:'¥' —— ¥
要求:
給定一個數(shù)字,顯示一下是中文的星期幾莱衩。
let n = 3;
{{n|cnDay}} —— 星期三
自定義過濾器:
angular.module('app',[])
.filter('cnDay',function(){
return function(input){
//input 就是要處理的輸入的數(shù)據(jù)
//輸入的數(shù)據(jù)——要對誰使用這個過濾器
//對input進行處理
return '返回值——就是最終要使用的內容';
};
});
四爵嗅、自定義指令
指令: ng-bind ng-model....
以ng-開頭的都是系統(tǒng)提供的指令
自定義一個指令:hehe-haha
angular.module('myapp',[])
.directive('heheHaha',()=>{
return {
template:'<span>歡迎你</span>'
};
})
指令名稱:
駝峰命名法: 第一個字母小寫,以后每個單詞首字母大寫
限制選項:restrict —— 默認值 A E
restrict:字符串
A: Attribute 屬性
E: Element 元素
C: Class 類笨蚁,樣式中的class
M: coMment 注釋 : 需要配合 replace:true
***M的時候 兩邊要留空格
自定義指令中常用的幾個選項:
template: 輸出的模板
restrict: 限制睹晒、約束
ECMA
replace: 默認是false,
設置true括细,替換原有的標簽