首先,我不认为替换:在不久的将来,真正被删除。它已被弃用,但在任何角度1.X版本中摆脱它将是一个突破性的改变,所以我认为你没事,直到发布Angular 2.0。 Angular 2.0将打破更多,所以我不会太担心如何让现有的应用程序工作,而不需要更换:true。很可能2.0会具有某种等效的功能,但将现有的应用程序迁移到2.0可能不是您想要做的事情。
然而,假设回答你的问题:
取代:真正做了两两件事:
1)替换元素与指令模板的DOM。
2)它将DOM中元素的属性与指令模板合并。
(2)实际上是该特征的非平凡部分,它周围的边缘情况是Angular团队想要摆脱它的原因。
举例来说,如果你的元素是:
<div some-directive ng-click="doSomething()"><div>
而且someDirective有一个模板:
<div ng-click="doSomethingInDirective()"></div>
你已经更换了节点之后的优先级? replace:true的逻辑当前会做出这些决定并为您提供一切。
如果出于某种原因将其替换为:真正从1.X中删除(它不会),那么你会怎么做?
答案是,你将不得不手工做的替换:真正做现在,这(简体)是:
1)获取的原始元素的属性
2)合并的原始元素与属性指令模板,跟踪不是指令和属性的属性,并且跟踪每个指令的声明位置(在原始元素或模板中)。
3)通过编译合并的属性指令和指令模板指令来编译指令模板。
4)将作为原始元素一部分的合并属性指令链接到$ parent作用域,并将所有其他指令链接到指令作用域。链接指令该指令范围
4A)如果必要的话(transclude执行transclusion:true和模板包括NG-transclude)
5)替换为完全挂钩指令模板原始元素。
这里是从GitHub的相关代码:
if (directive.replace) {
replaceDirective = directive;
if (jqLiteIsTextNode(directiveValue)) {
$template = [];
} else {
$template = removeComments(wrapTemplate(directive.templateNamespace, trim(directiveValue)));
}
compileNode = $template[0];
if ($template.length != 1 || compileNode.nodeType !== NODE_TYPE_ELEMENT) {
throw $compileMinErr('tplrt',
"Template for directive '{0}' must have exactly one root element. {1}",
directiveName, '');
}
replaceWith(jqCollection, $compileNode, compileNode);
var newTemplateAttrs = {$attr: {}};
// combine directives from the original node and from the template:
// - take the array of directives for this element
// - split it into two parts, those that already applied (processed) and those that weren't (unprocessed)
// - collect directives from the template and sort them by priority
// - combine directives as: processed + template + unprocessed
var templateDirectives = collectDirectives(compileNode, [], newTemplateAttrs);
var unprocessedDirectives = directives.splice(i + 1, directives.length - (i + 1));
if (newIsolateScopeDirective) {
markDirectivesAsIsolate(templateDirectives);
}
directives = directives.concat(templateDirectives).concat(unprocessedDirectives);
mergeTemplateAttributes(templateAttrs, newTemplateAttrs);
我不会出去,很快写自己,任何时候的版本。任何需要替换的代码:true也需要Angular 1.X,所以它会被支持。当你准备用Angular 2.0写一个新的应用程序时,会有另一种(希望更好)的方式来做到这一点。
“标签对浏览器没有任何意义”......但不幸的是它对其他指令有意义。例如,标签内的任何内容都不再可用于Angular的表单验证。所以有一个坚实的用例来“替换”。 transclude是否提供相同的功能? – 2015-07-09 01:44:23
“..对浏览器没有意义..”,取决于你所指的意义。我遇到了指令元素缠绕
它也不会做属性合并,这对于我制作包装文件输入的组件时非常重要。 – Thayne 2016-04-29 23:44:39
相关问题