AngularJS:调用模板子元素上的可选嵌套指令
AngularJS : invoking optional nested directive on template sub-element
我正试图编写一个指令,为表单中的字段发出所有HTML——包装div、标签、输入等。对于某些字段,我想使用Angular UI Bootstrap的"typeahead"指令。
我第一次尝试在模板中使用ng attr typeahead="{{myTypeahead}}"。我预计,在没有设置"myTypeahead"的字段上,不会有"typeahead"属性的证据。相反,在指令处理过程中,属性以未定义的值出现在属性列表中,并且typeahead指令会被调用,并由于其输入未定义而立即中断。
然后我尝试使用一个后编译函数,比如:
compile: function compile(tElement, tAttrs, transclude) {
return function postLink(scope, iElement, iAttrs, controller) {
if ( iAttrs.eiTypeahead !== undefined ) {
var me = $(iElement);
var input = $("input", me);
input.attr("typeahead", iAttrs.eiTypeahead);
$compile(input[0]);
}
}
}
这将"typeahead"属性放在输入元素上,但不调用typeahead指令。
我想这可能是其他帖子的副本,但我没有搜索到合适的单词来找到它。
当您将其他指令添加到指令元素时,这些指令应该从指令的compile
函数添加,并且您可以从编译返回的postLink
函数编译指令元素。
代码
compile: function compile(tElement, tAttrs, transclude) {
if ( iAttrs.eiTypeahead !== undefined ) {
var me = $(iElement);
var input = $("input", me);
input.attr("typeahead", iAttrs.eiTypeahead);
}
return function postLink(scope, iElement, iAttrs, controller) {
$compile(input[0]);
}
}
您可以参考这个答案r来更好地解释
相关文章:
- 由于$compile,Javascript(Angular)嵌套指令加载了两次
- 角度嵌套指令回调
- 重新编译AngularJS嵌套指令
- Angularjs.嵌套指令中的继承作用域
- 基于嵌套指令Angular的宽度更改文本
- 使用 ng-repeat呈现嵌套指令时出错
- 角度嵌套指令仅呈现第一个子指令
- 带有嵌套指令的 Angularjs
- AngularJS指令:将动态创建的对象作为嵌套指令的属性传递
- 嵌套指令 - 无法将 args 从 Angularjs 中的子指令传递给控制器方法
- 嵌套指令中的 AngularJS 格式化程序
- AngularJS嵌套指令入到它们假定的父元素之外
- 在指令和嵌套指令之间共享模型
- 如何在嵌套指令之间共享变量
- 嵌套指令未在加载时激发
- 正在检查嵌套指令中的已定义函数
- 为什么在使用controllerAs嵌套指令时父作用域会中断
- 访问嵌套指令中的父作用域变量
- AngularJS嵌套指令中的双向数据绑定
- 嵌套指令,子指令don't "transclude"正确