假设这个指令:
<validation-errors field="someField">Some errors: {{errors}}</validation-errors>
我想我可以像这样简单地创建指令函数:
return {
require: '^form',
restrict: 'E',
link: link,
scope: {
field: '@'
},
transclude: true,
template: '<span ng-show="errors" class="alert-danger" ng-transclude></span>'
};
function link(scope, el, attr, ctrl, transcludeFn) {
scope.errors = ctrl.Validator.getErrors(attr.field);
}
但是由于Transclusion is the process of extracting a collection of DOM element from one part of the DOM and copying them to another part of the DOM, while maintaining their connection to the original AngularJS scope from where they were taken.
(来自文档),范围不像我想象的那样工作。
所以我尝试了这个工作,除了“一些错误”部分是重复的:
transcludeFn(function(clone, transScope) {
scope.errors = transScope.errors = ctrl.Validator.getErrors(attr.field);
el.append(clone);
});
如果我删除它不起作用el.append(clone);
。
使嵌入内容共享指令模板范围的最佳方法是什么?