在另一个指令中嵌入AngularJS指令
Embed AngularJS directive in another directive
我正试图在一个新指令中实现Bootstrap Typeahed的一些自定义函数。为此,我需要将原始的Typeahed指令嵌入到我的指令中,并将其传递给我的参数:
这是最初的指令:
<div class="typeahead typeahead-lookup">
<input ng-model="vm.myModel"
uib-typeahead="item as item.Formatted for item in vm.refreshSearch($viewValue)"
typeahead-wait-ms="1000"
typeahead-min-length="1"
typeahead-editable="false"
placeholder="SEARCH..."
type="text"
class="form-control">
</div>
这是我的自定义指令:
<select-lookup model="vm.myModel"
items="item as item.Formatted for item in vm.refreshSearch($viewValue)"
</select-lookup>
这就是我试图实现它的方式:
function selectLookup($compile) {
return {
restrict: 'E',
scope: {
model: "=",
items: "@"
},
compile: function(element, attrs) {
return function(scope, element, attrs) {
var template = '<div class="typeahead typeahead-lookup">' +
'<input ng-model="model"' +
'uib-typeahead="{{items}}"' +
'typeahead-wait-ms="1000"' +
'typeahead-min-length="1"' +
'typeahead-editable="false"' +
'placeholder="SEARCH..."' +
'type="text"' +
'class="form-control">' +
'</div>';
element.html(template);
$compile(element.contents())(scope);
};
}
};
}
将参数从指令传递到原始Typeahead指令时遇到问题,尤其是"items"。我得到了这个错误:Expected typeahead specification in form of "_modelValue_ (as _label_)? for _item_ in _collection_" but got "{{items}}"
。
有人能帮我解决这个问题吗?
我会尝试'uib-typeahead=' + attrs.items +
而不是'uib-typeahead="{{items}}"'
Typeahead正在为"for in"语法使用自定义语法分析器。https://github.com/angular-ui/bootstrap/blob/master/src/typeahead/typeahead.js#L7
相关文章:
- 一次显示一个隐藏指令-AngularJS
- 一次显示一个指令AngularJS
- 在同一页面上多次使用的自定义指令 (AngularJS) 中的验证
- 无法绑定到没有指令 + angularjs 标识符的控制器
- 值未传递给指令-AngularJS
- 测试依赖于另一个指令Angularjs的指令
- 根据指令angularjs 1.2分配控制器
- 有模板问题的指令.AngularJS
- 编译指令在指令 AngularJS 中
- 我可以将 templateUrl 传递给指令 - AngularJS
- 从子指令的父指令 [angularJS] 触发子指令中的函数
- 将服务注入指令 AngularJS 和数据绑定
- 在指令 AngularJS 中解析
- 从指令 AngularJS 给出高图表正确的参数
- 获取 JSON 数据并传递给指令 Angularjs
- 点击事件后的渲染模板指令 - AngularJs
- 如何从分页指令angularjs隐藏直接页面链接
- AngularJS-关闭tile并从ngRepeat指令AngularJS中的模型加载新视图
- 触发指令AngularJs的控制器作用域
- 将定义的对象传递给另一个自定义指令angularjs