代码如下:

var infographicModule = angular.module('infographicModule',[]); 
infographicModule.directive('bodyTextInfographic', ['$compile','$timeout','jsonService',function($compile,$timeout,jsonService) {

    return{
        restrict: 'C',
        
        link: function (scope, iElement, iAttrs) {
            var tempURL=scope.source['default'].href || scope.source['medium'].href || scope.source['small'].href;
            
            if(tempURL){
                
                var resultingElement = "<div class='infographic_iframe_container'>"+
                                            "<iframe onLoad='infoloadfun()' class='infographic_iframe' frameborder='0' scrolling='auto' src="+ tempURL +"></iframe>"+
                                        "</div>";
                
                
                var linkFn = $compile(resultingElement);
                var newElement = linkFn(scope);
                iElement.replaceWith(newElement); //这里的iElement也可以替换成页面上的任意元素
            }
        }
    }

}]);

//另一使用示例:(
iElement也可以替换成页面上的任意元素
jqEle.html(tpl);

相比传统的template,$compile更为灵活

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-06-04
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-05-25
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-01-05
  • 2021-06-26
  • 2021-10-26
  • 2021-06-08
  • 2022-12-23
相关资源
相似解决方案