1.provider
<div ng-controller="twoCtrl"></div>
<script>
var app=angular.module('appModule',[]);
app.config(function (CalcProvider) {//CalcProvider 是Calc声明的函数的实例 就是下边的this
CalcProvider.currency='RMB'; //会等provider创建好实例后才能配置
});
//localStorage
app.provider('Calc',function () {
this.currency='$';
this.$get=function () { //构造函数 this在哪个函数中就是哪个函数的实例
return {//返回一个对象,用来公用的对象
'+':(a, b) =>{
return this.currency+(a+b)
}
}
}
});
app.controller('oneCtrl',function ($scope,Calc) {
console.log(Calc['+'](1,2))
});
<div ng-controller="oneCtrl"></div>
app.factory('Calc',function () {//后面的函数是provider中$get方法对应的
this.currency='RMB';
return {
'+':(a, b) =>{
return this.currency+(a+b)
}
}
});
app.controller('oneCtrl',function ($scope,Calc) {
console.log(Calc['+'](1,2))
});
<div ng-controller="oneCtrl"></div>
<script>
var app=angular.module('appModule',[]);
app.service('Calc',function () {
this['+']=(a,b)=>a+b;
//factory返回的对象,是service函数实例化后的结构
/* return {
'+':(a, b) =>{
return this.currency+(a+b)
}
}*/
});//后面的函数就是$get函数
app.controller('oneCtrl',function ($scope,Calc) {
console.log(Calc['+'](1,2))
});
</script>
value:
<div ng-controller="oneCtrl"></div>
<script>
var app=angular.module('appModule',[]);
app.value('Calc', {
'+':(a,b)=>a+b,
'-':(a,b)=>a-b
});
app.controller('oneCtrl',function ($scope,Calc) {
console.log(Calc['+'](1,2))
});
</script>
contnet:
<div ng-controller="oneCtrl"></div>
<script>
var app=angular.module('appModule',[]);
app.constant('Calc', {
'+':(a,b)=>a+b,
'-':(a,b)=>a-b
});
app.controller('oneCtrl',function ($scope,Calc) {
console.log(Calc['+'](1,2))
});
</script>
本地存储(Storage)
<script>
var app = angular.module('appModule', []);
app.provider('Storage', function () {
this.$get = function () {
return {
setStorage: function (key, value) {
if (typeof value == 'object') {
value = JSON.stringify(value)
}
localStorage.setItem(key, value)
},
getStorage: function (key) {
var str=localStorage.getItem(key);
if(str.startsWith('{')||str.startsWith('[')){
str=JSON.parse(str);
}
return str
}
}
}
});
app.controller('oneCtrl', function ($scope,Storage) {
Storage.setStorage('name', {name: 'www'});
});
app.controller('twoCtrl', function ($scope,Storage) {
console.log(Storage.getStorage('name').name)
})
</script>
事件交互
$scope.$on('买了雪碧',function (e,data) {
$scope.total=data;
console.log(data);
});
$scope.$watch('count',function () {
$scope.$emit('买了雪碧',$scope.count*$scope.price);//数量变化向上发射
});
$scope.$watch('count',function () {
$scope.$emit('买了雪碧',$scope.count*$scope.price);//数量变化向上发射
});
$broadcast 向下发射包括自己
$scope.$watch('total',function () {
$scope.$broadcast('总共',$scope.total)
})
$interval\$timeout
{{date | date:'hh:mm:ss'}}
<script>
var app=angular.module('appModule',[]);
app.controller('myCtrl',function ($scope,$interval,$timeout) {//$interval单次 $timeout多次
//不是angular中的方法中改变数据不会刷新视图
var timer=$interval(function () {//angular中提供的服务都可以自动调用$apply,不需要手动调用
$scope.date=Date.now();
$interval.cancel(timer);//取消定时器
});
},1000);
</script>
$Apply
<input type="text" zf-model="aa">{{aa}}
var app=angular.module('appModule',[]);
app.run(function ($rootScope) {
$rootScope.aa='zfpx'
});
app.directive('zfModel',function () {
return {
restrict:'A',
link:function (scope,element,attrs) {
element.on('input',function () {//监听输入框中的输入事件
// scope.age=element.val();
scope[attrs.zfModel]=element.val();
scope.$apply();//强制让视图刷新
});
scope.$watch(attrs.zfModel,function (newVal) {
element.val(newVal);
})
}
}
})