Rumah  >  Soal Jawab  >  teks badan

javascript - ng-repeat怎么实现的?

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

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

PHP中文网PHP中文网2748 hari yang lalu485

membalas semua(1)saya akan balas

  • 伊谢尔伦

    伊谢尔伦2017-04-10 14:35:42


    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); }

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

    balas
    0
  • Batalbalas