登录

angular.js - 指令里生成的html怎么解析

return {
            restrict: 'EA',
            link: function (scope, element, attr) {
                element.bind('mouseenter', function() {
                    this.after("<p style='position: absolute;'><img src=" + this.src + " /></p>")
                });
            }
        }

比如像上边这样,使用jquery的after方法,这个p在页面显示成了字符串,怎么才能直接解析出来?

# AngularJS
世界只因有你世界只因有你1913 天前429 次浏览

全部回复(2) 我要回复

  • 天蓬老师

    天蓬老师2017-05-15 17:14:05

    利用 $compile

    写了个在线小demo

    http://embed.plnkr.co/egEOkZv...

    回复
    0
  • 淡淡烟草味

    淡淡烟草味2017-05-15 17:14:05

    题主的引号问题导致的

    this.after("<p style='position: absolute;'><img src=" + this.src + " /></p>")
    

    很明显这里的url少了个引号呢、img的src要用引号包裹、可以加单引号在两边

    this.after("<p style='position: absolute;'><img src='" + this.src + "' /></p>")
    

    你在页面上的字符串也是src没引号

    回复
    0
  • 取消回复发送