【发布时间】:2015-07-10 15:51:34
【问题描述】:
假设这个指令:
<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);,它将不起作用。
让嵌入的内容共享指令模板范围的最佳方法是什么?
【问题讨论】:
标签: angularjs angularjs-directive angularjs-ng-transclude