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. javascript - 如何將一個div始終固定在某個位置;無論屏幕和分辨率怎么變化;div位置始終不變2. html - vue項目中用到了elementUI問題3. javascript - 原生canvas中如何獲取到觸摸事件的canvas內坐標?4. javascript - vscode alt+shift+f 格式化js代碼,通不過eslint的代碼風格檢查怎么辦。。。5. javascript - 求解答:實例對象調用constructor,此時constructor內的this的指向?6. javascript - 有什么比較好的網頁版shell前端組件?7. java - 如何寫一個intellij-idea插件,實現編譯時修改源代碼的目的8. javascript - [js]為什么畫布里不出現圖片呢?在線等9. java 中Long 類型如何轉換成Double?10. html5 - 有可以一次性把所有 css外部樣式轉為html標簽內style=" "的方法嗎?
