-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path5-parse.html
108 lines (83 loc) · 2.44 KB
/
5-parse.html
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<!DOCTYPE html>
<html ng-app="plunker">
<head>
<meta charset="utf-8"/>
<title>AngularJS Plunker</title>
<link rel="stylesheet" href="style.css"/>
<script src="angular.js"></script>
</head>
<body>
<div test="1 + 1;"></div>
<div test=".1 + 1"></div>
<div test="{a:1, b: 2}"></div>
<div test="{a:1 + 1, b: 2}"></div>
<div>{{ a.b.c }}</div>
</body>
<style>
.ng-scope {
border: solid 1px #0066cc;
margin: 5px;
}
</style>
<script>
var app = angular.module('plunker', [])
.directive('test', function ($parse) {
return function (scope, elem, attrs) {
console.log('test ', $parse(attrs['test'])());
}
});
var $injector = angular.injector(['ng']);
// basic
$injector.invoke(function ($parse) {
console.log($parse('1 + 1')());
});
$injector.invoke(function ($parse) {
console.log($parse('{a:1 + 2 * (3 / 4), b:2};', false)());
});
$injector.invoke(function ($parse) {
var getter = $parse('user.name');
var setter = getter.assign;
var context = {user: {name: 'angular'}};
var locals = {user: {name: 'local'}};
console.log(getter(context, locals));
console.log(getter(context));
setter(context, 'newValue')
});
// ++ 操作不存在
// $injector.invoke(function($parse) {
// console.log($parse('1++', false)());
// });
// 一元操作符
$injector.invoke(function ($parse) {
console.log($parse('+1')());
console.log($parse('-1')());
console.log($parse('+ { a: 1 }')());
console.log($parse('!false')());
});
$injector.invoke(function ($parse) {
console.log($parse('a.b.c.d.e', false)({ a: { b: { c: { d: { e: 'hello' } } } } }));
});
$injector.invoke(function ($parse) {
console.log($parse(" a = 'c' + 'd' | uppercase ")({ a: 'a' }));
});
// filter 的正确用法
$injector.invoke(function ($parse) {
//console.log($parse(" 'a' | uppercase | aFilter | bFilter")());
// this is not correct
// console.log($parse(" { a: 'a' | uppercase } ")());
// console.log($parse(" 'a' | uppercase + 'b' | uppercase ")());
console.log($parse(" { a: ('a' | uppercase) } ")());
console.log($parse(" [ ('a' | uppercase) ] ")());
});
// = 的用法
$injector.invoke(function ($parse) {
// 错误
// console.log($parse(" a = b = 'c' ")({ a: 'a', b: 'b' }));
});
// ?: 的用法
$injector.invoke(function ($parse) {
// 错误
console.log('?:', $parse(" 0 ? 2 : 3 ? 4 : 5 ")());
});
</script>
</html>