我遇到了一个与Angular相关的小问题,但无法解决。我怀疑这可能与作用域有关,但我并不完全确定。
以下是我的HTML代码:
<i class="icon-question-sign" popover data-placement="top" data-trigger="hover" data-title="Top 10 clients" data-content-compile="<ul><li ng-repeat='client in user.clients | limitTo: 10'>{{client}}</li></ul>"></i>
这里是我的指令:
app.directive('popover', function($timeout, $compile) {
var linker = function (scope, element, attrs) {
$timeout(function() {
var content = $compile(element.data('content-compile'))(scope);
element.popover({
'html': true,
'content': content
});
}, 200);
}
return {
restrict: 'A',
link: linker
}
});
结果正确地重复了li,长度为{{user.clients}},但没有呈现{{client}}。出于某种原因,它显示为空白,然而它有一个字符串值,并且在直接添加到HTML并未经过指令编译时可以正常工作。它当前在DOM中的显示如下:
<ul class="ng-scope"><!-- ngRepeat: client in user.clients | limitTo: 10 --><li ng-repeat="client in user.clients | limitTo: 10" class="ng-scope"></li><li ng-repeat="client in user.clients | limitTo: 10" class="ng-scope"></li><li ng-repeat="client in user.clients | limitTo: 10" class="ng-scope"></li><li ng-repeat="client in user.clients | limitTo: 10" class="ng-scope"></li><li ng-repeat="client in user.clients | limitTo: 10" class="ng-scope"></li><li ng-repeat="client in user.clients | limitTo: 10" class="ng-scope"></li><li ng-repeat="client in user.clients | limitTo: 10" class="ng-scope"></li><li ng-repeat="client in user.clients | limitTo: 10" class="ng-scope"></li><li ng-repeat="client in user.clients | limitTo: 10" class="ng-scope"></li><li ng-repeat="client in user.clients | limitTo: 10" class="ng-scope"></li></ul>
如果我将{{client}}替换为{{user.email}},它会正确列出。不确定这里出了什么问题 - 我可能漏掉了一些明显的东西!