You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This example defines a simple directive, which is a UI component. The defined component (called "zippy") has header and content. Click on its header toggles the visibility of the content.
952
952
-->
@@ -955,27 +955,42 @@ This example defines a simple directive, which is a UI component. The defined co
955
955
<!--
956
956
From the first example we can note that the whole DOM tree is a composition of elements. The root component is the `html` element, directly followed by the nested elements `head` and `body` and so on...
In the second, JavaScript, example we see that the `template` property of the directive, contains markup with `ng-transclude` directive inside it. So this means that inside the directive `zippy` we have another directive called `ng-transclude`, i.e. composition of directives. Theoretically we can nest the components infinitely until we reach a leaf node.
>In computer programming, the interpreter pattern is a design pattern that specifies how to evaluate sentences in a language. The basic idea is to have a class for each symbol (terminal or nonterminal) in a specialized computer language. The syntax tree of a sentence in the language is an instance of the composite pattern and is used to evaluate (interpret) the sentence.
Behind its `$parse` service, AngularJS provides its own implementation of interpreter of a DSL (Domain Specific Language). The used DSL is simplified and modified version of JavaScript.
971
975
The main differences between the JavaScript expressions and AngularJS expressions that AngularJS expressions:
976
+
-->
977
+
978
+
- 可以包含 UNIX 类管道语法的过滤器
979
+
- 不会抛出任何错误
980
+
- 不含任何控制流语句 (异常、循环、条件语句,但可以使用三元运算符)
981
+
- 在特定的上下文环境中进行解析估值 (当前 `$scope` 的上下文)
972
982
983
+
<!--
973
984
- may contain filters with UNIX like pipe syntax
974
985
- don't throw any errors
975
986
- don't have any control flow statements (exceptions, loops, if statements although you can use the ternary operator)
976
987
- are evaluated in given context (the context of the current `$scope`)
988
+
-->
977
989
990
+
`$parse` service 中定义了两个主要的组件:
991
+
<!--
978
992
Inside the `$parse` service are defined two main components:
993
+
-->
979
994
980
995
```JavaScript
981
996
//Responsible for converting given string into tokens
@@ -984,9 +999,15 @@ var Lexer;
984
999
var Parser;
985
1000
```
986
1001
1002
+
当给定的表达式被分词后,出于性能需求会被内部缓存。
1003
+
<!--
987
1004
Once given expression have been tokenized it is cached internally, because of performance concerns.
1005
+
-->
988
1006
1007
+
AngularJS DSL 中的终结符表达式定义如下:
1008
+
<!--
989
1009
The terminal expressions in the AngularJS DSL are defined as follows:
1010
+
-->
990
1011
991
1012
```JavaScript
992
1013
varOPERATORS= {
@@ -1019,32 +1040,56 @@ var OPERATORS = {
1019
1040
};
1020
1041
```
1021
1042
1043
+
我们可以将每个终结符所属函数看作是 `AbstractExpression` 接口的实现。
1044
+
<!--
1022
1045
We can think of the function associated with each terminal as implementation of the `AbstractExpression`'s interface.
1046
+
-->
1023
1047
1048
+
每个 `Client` 在一个特定的上下文和作用域中解释给定的 AngularJS 表达式。
1049
+
<!--
1024
1050
Each `Client` interprets given AngularJS expression in a specific context - specific scope.
1051
+
-->
1025
1052
1053
+
以下是 AngularJS 表达式:
1054
+
<!--
1026
1055
Few sample AngularJS expressions are:
1056
+
-->
1027
1057
1028
1058
```JavaScript
1029
1059
// toUpperCase filter is applied to the result of the expression
渲染动态页面并不是件简单的事情,包含大量字符串的连接、修改和一些难以解决的操作。创造动态页面的最简单的方法是编写自己的标记符号并在其中嵌入表达式。稍后在给定的上下文中对这些表达式进行解析,从而将整个模版编译成其最终格式,此处即为 HTML (或 DOM) 格式。这就是模版引擎的功用 - 取得给定的 DSL,在恰当的上下文中进行解析,并转换成其最终格式。
1074
+
<!--
1040
1075
The dynamic page rendering is not that trivial thing. It is connected with a lot of string concatenations, manipulations and frustration. Far easier way to build your dynamic page is to write your markup and embed little expressions inside it, which are lately evaluated in given context and so the whole template is being compiled to its end format. In our case this format is going to be HTML (or even DOM). This is exactly what the template engines do - they take given DSL, evaluate it in the appropriate context and then turn it into its end format.
Templates are very commonly used especially in the back-end.
1043
1081
For example, you can embed PHP code inside HTML and create a dynamic page, you can use Smarty or you can use eRuby with Ruby in order to embed Ruby code inside your static pages.
For JavaScript there are plenty of template engines, such as mustache.js, handlebars, etc. Most of these engines manipulate the template as a string. The template could be located in different places - as static file, which is fetched with AJAX, as `script` embedded inside your view or even inlined into your JavaScript.
1087
+
-->
1046
1088
1089
+
例如:
1090
+
<!--
1047
1091
For example:
1092
+
-->
1048
1093
1049
1094
```html
1050
1095
<scripttype="template/mustache">
@@ -1055,9 +1100,15 @@ For example:
1055
1100
</script>
1056
1101
```
1057
1102
1103
+
模版引擎在一个给定的上下文中编译此字符串,将其转换为 DOM 元素。如此,所有嵌在标记符中的表达式都会被解析,并替换成其计算值。
1104
+
<!--
1058
1105
The template engine turns this string into DOM elements by compiling it within a given context. This way all the expressions embedded in the markup are evaluated and replaced by their value.
AngularJS templates are actually HTML, they are not in an intermediate format like the traditional templates are.
1070
1123
What AngularJS compiler does is to traverse the DOM tree and look for already known directives (elements, attributes, classes or even comments). When AngularJS finds any of these directives it invokes the logic associated with them, which may involve evaluation of different expressions in the context of the current scope.
1124
+
-->
1071
1125
1126
+
例如:
1127
+
<!--
1072
1128
For example:
1129
+
-->
1073
1130
1074
1131
```html
1075
1132
<ulng-repeat="name in names">
1076
1133
<li>{{name}}</li>
1077
1134
</ul>
1078
1135
```
1079
1136
1137
+
在下列作用域的上下文中:
1138
+
<!--
1080
1139
in the context of the scope:
1140
+
-->
1081
1141
1082
1142
```javascript
1083
1143
$scope.names= ['foo', 'bar', 'baz'];
1084
1144
```
1085
1145
1146
+
会生成跟上面相同的结果。其主要区别是,模版是包装在 HTML 中,而非 `script` 标签之间。
1147
+
<!--
1086
1148
will produce the same result as the one above. The main difference here is that the template is not wrapped inside a `script` tag but is HTML instead.
0 commit comments