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. Python列表或者字典里面的中文如何處理?2. javascript - ES6 Module可以直接導入commonJS的模塊, 這個是ES6直接就實現了的, 還是babel轉的時候實現的?3. javascript - vue router 怎么實現某個頁面禁止瀏覽器回退?4. javascript - 【快速判斷數組為空】[]==false 為什么返回true?5. javascript - 請問,jquery中創建新節點之后插入之后再給這個節點添加css樣式,可以一句話寫完么?6. javascript - html 表單如何恢復7. javascript - 關于JS 事件委托操作ul li標簽的問題8. javascript - js的shift()方法失效?9. javascript - 關于禁用文本選擇與復制的問題10. javascript - 讀js權威指南“作為值得函數”產生了疑惑,求釋疑
