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. html5 - 最近在自學react 求一個react表單提交的例子2. javascript - jq 上傳圖片成功后添加一個新的上傳框時出現問題3. angular.js - 在終端中用yeoman啟用angular-generator報錯,求解?4. 老師您好!我有一個問題、5. 請問寫好python模塊以后,文檔怎么寫?6. javascript - 原生JS和jQuety關于設置圖片輪播定時器問題7. laravel5.8 前臺auth 登錄 不了8. jquery清除input type為password?9. 未定義數組索引: period,求大神指點10. javascript - 微信內置瀏覽器的ua是多少?
