我有下面的指令,我用它来生成链接(以及其他东西)。我在这里做了一些简化:
oActivityModule.directive('activityDetails', function () {
return {
restrict: 'E',
scope: {
activityLog: '@',
},
link: function(scope, element, attr) {
element.html(details(angular.fromJson(scope.activityLog)));
}
};
});
function details(oActivityLog) {
return '<a sx-sref="channels.channel({channelID: ' + oActivityLog.channelId + '})">' + oActivityLog.name + '</a>';
}
}问题是sx-sref标记没有被编译成href标记,它看起来像是在源html页面中:
<a sx-sref="channels.channel({channelID: 123})">A channel name</a>我的路线和一切都设置得很好。我在我的应用程序的其他部分使用sx-sref进行编译,但是在这些情况下,sx-sref是在一个模板html文件中,而不是一个angular指令js文件中。
注意,在上面的js指令文件中有更多的逻辑(未显示)来生成链接的数据,这就是为什么我不能将链接放在HTML中的原因。
也许这与element.html有关(...)打电话?
element.html(details(angular.fromJson(scope.activityLog)));发布于 2016-12-01 18:39:19
解决方案是注入作用域,将其作为参数传递给指令函数,然后对字符串调用$compile (...)($compile):
oActivityModule.directive('activityDetails', ['$compile' function ($compile) {
return {
restrict: 'E',
scope: {
activityLog: '@',
},
link: function(scope, element, attr) {
$compile(element.html(details(angular.fromJson(scope.activityLog))))(scope);
}
};
}]);https://stackoverflow.com/questions/40887243
复制相似问题