我无法让 $compile 在我的指令中正常工作。我正在尝试将 html 插入到带有 ng-bind 指令的元素中。我将变量从选择器服务传递到 ng-bind。
当我显示 html 时,绑定(bind)不起作用。相反,我得到了空元素。编译后的 Html 如下所示:
<div class = "song_info ng-binding ng-scope" data-ng-bind = "year"></div>
这是我正在使用的指令:
.directive("sortableQueue", ["$compile", "selector", function($compile, selector){
return{
scope : {},
link : function(scope, element){
element.sortable({
stop : function(event, ui){
//get helper html
var song_element = $(ui.item);
//logs correct value
console.log(selector.getValue());
var html = "<div class = 'song_info' data-ng-bind = '" + selector.getValue() + "'></div>";
var content = $compile(html)(scope);
song_element.html(content);
}
});
}
};
}])
有人知道我在这里犯了什么错误吗?如有任何帮助,我们将不胜感激。
最佳答案
一切正常。
答案在这里:
data-ng-bind = "year"
Angular 尝试查找年份变量。当然它找不到它并且什么也不显示。您必须在您的范围内创建变量,从中获取数据。
关于javascript - AngularJS $compile 无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37991398/