<!DOCTYPE html>
<html lang="zh" ng-app="myapp">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<script src="../bower_components/angular/angular.js" type="text/javascript" charset="utf-8"></script>
<title>AngularJs-指令</title>
<style type="text/css">
.red{color:red};
.green{color:green};
</style>
</head>
<body>
<h3>AngularJs-$watch-01</h3>
<input type="text" ng-model="username" />
{{username}}
<p>改变的次数为 : {{count}}</p>
<h3>AngularJs-$watch-02</h3>
<div ng-repeat="item in items">
<input type="text" ng-model="item.value" />{{item.value}}
</div>
<p>改变的次数为 : {{rcount}}</p>
<h3>AngularJs-$watchCollection</h3>
<a href="javascript:void(0);" ng-click="addvalue()">add</a>
<a href="javascript:void(0);" ng-click="delvalue()">del</a>
<a href="javascript:void(0);" ng-click="unwatch()">unwatch</a>
<div ng-repeat="item in items">
<input type="text" ng-model="item.value"/>{{item.value}}
</div>
<h3>AngularJs-ng-switch</h3>
<!--不可见-->
<div ng-controller="ifController">
<div ng-switch on="age">
<span ng-switch-when="10">青年1</span>
<span ng-switch-when="20">青年2</span>
<span ng-switch-when="30">青年3</span>
<span ng-switch-when="40">青年4</span>
<span ng-switch-default>青年5</span>
</div>
<h3>AngularJs-ng-if</h3>
<div>
<span ng-if="age>=60 && age<80">老年</span>
<span ng-if="age>=40 && age<60">壮年</span>
<span ng-if="age>=30 && age<40">青年</span>
<span ng-if="age<10 && age<30">少年</span>
</div>
</div>
<script type="text/javascript">
var app = angular.module("myapp",[]);
// run主要作用为代表angularjs装在完毕某一些内置对象的时候
// 可能会执行以下回调函数,回调就初始化代码.
/*app.run(function($rootScope){
$rootScope.username = "bilaisheng";
});*/
app.run(["$rootScope", "$log", function($rootScope, $log){
$rootScope.username="bilaisheng";
$rootScope.count = 0;
$rootScope.$watch("username",function(){
$rootScope.count++;
});
}]);
app.run(["$rootScope","$log",function($rootScope,$log){
$rootScope.items = [
{value:1},
{value:2},
{value:3},
{value:4}
];
$rootScope.rcount = 0;
$rootScope.$watch("items", function(){
$rootScope.rcount++;
},true);// 为fasle: "引用监视". true: 全等监视
// watchCollection
$rootScope.wcount = 0;
var unWatcher = $rootScope.$watchCollection("items",function(){
$rootScope.wcount++;
});
}]);
app.run(["$rootScope","$log",function($rootScope,$log){
$rootScope.items = [
{value:1},
{value:2},
{value:3},
{value:4}
];
//$watchCollection 方法用来针对数组或者集合进行监视,他的性能介于全等监视和引用监视之间
//但是它并不会对数组中的每一项的属性进行监视,但是可以对数组的项目增减做出反应,
$rootScope.count = 0;
var unWatcher = $rootScope.$watchCollection("items",function(){
$rootScope.count++;
});
//添加值
$rootScope.addvalue = function(){
$rootScope.items.push({value:5});
};
//删除值
$rootScope.delvalue = function(){
$rootScope.items.splice(0,1);
};
//删除监听
$rootScope.unwatch = function(){
unWatcher();//解除监听
};
}]);
/**
* 1: 选中的元素:
* checkbox radio
* ng-checked = true/false
* select
* ng-selected = true/false
* 禁止选择:
* ng-disabled=true/false
* 2:关于指令如果调用作用域属性和方法的时候是不需要表达式的
*
*/
app.controller("ifController",function($scope){
$scope.age = 30;
});
</script>
</body>
</html>