-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathangularjs_expression.html
More file actions
87 lines (84 loc) · 3.09 KB
/
angularjs_expression.html
File metadata and controls
87 lines (84 loc) · 3.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>AngularJS·Hello AngularJS</title>
<link rel="stylesheet" href="http://cdn.bootcss.com/bootstrap/3.3.2/css/bootstrap.css">
<style>
.demo{
margin-bottom:10px;
border-bottom:1px solid #1b926c;
}
/*.navbar{*/
/*border:1px solid #1b926c;*/
/*background-color: #1fa67a;*/
/*}*/
</style>
</head>
<body>
<div class="container">
<div class="row">
<nav class="navbar navbar-default navbar-static-top">
<div class="container-fluid">
<div class="navbar-header">
<a class="navbar-brand" href="../index.html">首页</a>
</div>
<ul class="nav navbar-nav">
<li class="active"><a href="javascript:void(0)">Link</a></li>
<li><a href="javascript:void(0)">Link2</a></li>
</ul>
</div>
</nav>
</div>
<div ng-app="myApp">
<div class="row demo" ng-controller="ExpressionController">
<h3>Demo1: $watch使用</h3>
<input ng-model="expr" type="text" placeholder="Enter an expression"/>
<h5>解析结果:<b>{{parseResult}}</b></h5>
</div>
<div class="row demo" ng-controller="EmailController">
<h3>Demo1:$interpolate使用,邮件</h3>
<input ng-model="to" type="text" placeholder="Email"/>
<br/>
<textarea ng-model="emailBody" name="emailBody" id="" cols="30" rows="10"></textarea>
<h5>解析结果:</h5>
<pre>{{ previewText }}</pre>
</div>
</div>
</div>
</body>
</html>
<script src="http://cdn.bootcss.com/angular.js/1.3.8/angular.js" type="text/javascript"></script>
<script>
/**
* AngularJS通过$parse这个内部服务来进行表达式的运算,这个服务能够访问当前所处的作
用域。这个过程允许我们访问定义在$scope上的原始JavaScript数据和函数。
将$parse服务注入到控制器中,然后调用它就可以实现手动解析表达式。
* @type {*}
*/
var myApp=angular.module("myApp",[]);
myApp.controller("ExpressionController",function($scope,$parse){
$scope.$watch('expr',function(newVal,oldVal,scope){
if(newVal!=oldVal){
//用新表达式设置parseFun
var parseFun=$parse(newVal);
//获取解析过后的值
$scope.parseResult=parseFun(scope);
}
});
});
/**
* 在AngularJS中,我们的确有手动运行模板编译的能力。例如,插值允许基于作用域上的某
个条件实时更新文本字符串。
要在字符串模板中做插值操作,需要在你的对象中注入$interpolate服务。
*/
myApp.controller("EmailController",function($scope,$interpolate){
// 设置监听
$scope.$watch("emailBody",function(body){
if(body){
var template=$interpolate(body);
$scope.previewText=template({to:$scope.to});
}
});
});
</script>