javascript - ng-bind-html中 自定义的指令 不生效!
问题描述
问题:使用ng-bind-html 页面上已经生成了正确的html代码,但是标签中的 指令 不生效!js代码:
html代码:
问题解答
回答1:当然无法生效,ng-bind-html 等同于 innerHTML。
可以自定义一个类似 ng-bind-html-compile 的指令:
.directive(’bindHtmlCompile’, [’$compile’, function ($compile) {return { restrict: ’A’, link: function (scope, element, attrs) {scope.$watch(function () { return scope.$eval(attrs.bindHtmlCompile);}, function (value) { // In case value is a TrustedValueHolderType, sometimes it // needs to be explicitly called into a string in order to // get the HTML string. element.html(value && value.toString()); // If scope is provided use it, otherwise use parent scope var compileScope = scope; if (attrs.bindHtmlScope) {compileScope = scope.$eval(attrs.bindHtmlScope); } $compile(element.contents())(compileScope);}); }}; }]);
<p ng-bind-html-compile='getId(xxx)'></p>
相关文章:
1. Echart X坐标轴名称放在左边与Y坐标抽刻度重叠,如何解决?2. javascript - 微信小程序里怎么把页面转成图片分享3. 求助一个Android控件名称4. css - .clearfix:after中为什么设置display: table5. javascript - iframe 为什么加载网页的时候滚动条这样显示?6. 前端 - webapp 通过angular来做界面切换,能否做成native应用的效果?7. vim - docker中新的ubuntu12.04镜像,运行vi提示,找不到命名.8. IOS app应用软件的id号怎么查询?比如百度贴吧的app-id=4779278139. html5 - mui dialog 如何配置type属性10. html5 - 小程序的swiper那个点可以给他居右?
