javascript - ng-repeat怎么实现的?
PHP中文网
PHP中文网 2017-04-10 14:33:42
[JavaScript讨论组]

最近在重造一个ng-repeat的轮子,正则表达式实在有点难,只能在这里践行下拿来主义。要有实现嵌套循环的。
可以改造下:
<p ng-repeat="v in vs">{{v}}</p endrepeat>

{{}}表达式我已经实现。
跪求原理或者您的方案,最好能说下一些正则的写法。

PHP中文网
PHP中文网

认证高级PHP讲师

全部回复(1)
伊谢尔伦

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

<code class="lang-javascript"><br>var match = expression.match(/^\s*([\s\S]+?)\s+in\s+([\s\S]+?)(?:\s+as\s+([\s\S]+?))?(?:\s+track\s+by\s+([\s\S]+?))?\s*$/);

 

if (!match) {

  throw ngRepeatMinErr('iexp', "Expected expression in form of '_item_ in _collection_[ track by _id_]' but got '{0}'.",expression);

}

 

var lhs = match[1];

var rhs = match[2];

var aliasAs = match[3];

var trackByExp = match[4];

 

match = lhs.match(/^(?:([\$\w]+)|\(([\$\w]+)\s*,\s*([\$\w]+)\))$/);

 

if (!match) {

  throw ngRepeatMinErr('iidexp', "'_item_' in '_item_ in _collection_' should be an identifier or '(_key_, _value_)' expression, but got '{0}'.",lhs);

}

var valueIdentifier = match[3] || match[1];

var keyIdentifier = match[2];

 

if (aliasAs && (!/^[$a-zA-Z_][$a-zA-Z0-9_]*$/.test(aliasAs) ||

  /^(null|undefined|this|\$index|\$first|\$middle|\$last|\$even|\$odd|\$parent)$/.test(aliasAs))) {

  throw ngRepeatMinErr('badident', "alias '{0}' is invalid --- must be a valid JS identifier which is not a reserved name.",aliasAs);

}

 

</code>

这几段代码是用来判断ng-repeat表达式是否合法,并从中提取变量名的。。。反正我是没有耐心看这些正则表达式。

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号