来自 Bootstrap-UI Typeahead 模板的访问控制器范围 [英] access controller scope from Bootstrap-UI Typeahead template
问题描述
我无法使用 ui-typeahead 从自定义模板内部调用控制器函数:
I am unable to call a controller function from inside a custom-template with ui-typeahead:
<input typeahead="val for val in autoComplete($viewValue)"
typeahead-template-url="searchAutocompleteTpl.html"
ng-model="query"/>
<script type="text/ng-template" id="searchAutocompleteTpl.html">
<span ng-repeat="eqp in match.model.equipment"/>
<a href="" ng-click="showItem(eqp.model)">
found in: {{eqp.model}}
</a>
</script>
问题在于模板中似乎没有控制器的作用域:
The problem is that the controller's scope seems to be absent in the template:
showItem(eqp.model)
永远不会被调用.我也试过:
is never called. I have also tried with:
$parent.showItem(eqp.model)
无济于事.
那么如何在控制器的作用域内调用函数/值?
How can I call a function/value on the controller's scope then?
推荐答案
我遇到了同样的问题并查看了 github 上的 typeahead 代码 看看是否可以提供任何线索.似乎建议列表的创建涉及多个指令,每个指令都有自己的子作用域.
I ran into the same problem and had a look at the typeahead code on github to see if that might offer any clues. It appears that there are several directives involved in the creation of the suggestions list and each gets its own child scope.
换句话说,你的 $parent.showItem(eqp.model)
是一个很好的尝试,但你没有提升足够的水平.对我有用的是:$parent.$parent.$parent.showItem(eqp.model)
In other words, your $parent.showItem(eqp.model)
was a good attempt, but you didn't go up enough levels. What worked for me was: $parent.$parent.$parent.showItem(eqp.model)
这篇关于来自 Bootstrap-UI Typeahead 模板的访问控制器范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!