【问题标题】:How would an ngBindCompile directive be written?如何编写 ngBindCompile 指令?
【发布时间】:2013-06-09 12:59:06
【问题描述】:

如何编写类似于 ngBindTemplate 的指令,而不是采用作为模板的字符串,而是采用包含模板的变量?即:

现有:

ng-bind-template="{template}"

写作:

 ng-bind-compile="var"

在哪里var="{template}"

提前致谢!

【问题讨论】:

    标签: javascript html angularjs angularjs-directive angularjs-compile


    【解决方案1】:

    在指令的父范围的上下文中使用$compile 可以做到这一点:

    app.directive('ngBindCompile',function($compile){
        return {
            scope:{
                template: '=ngBindCompile'
            },
            link: function(scope,element,attrs){
                var html = '<div>' + scope.template + '</div>';
                var compiled = $compile(html)(scope.$parent);
                element.replaceWith(compiled);
            }
        }
    });
    

    Here is a fiddle

    【讨论】:

      猜你喜欢
      • 2013-01-15
      • 2015-02-16
      • 2016-12-13
      • 1970-01-01
      • 1970-01-01
      • 2015-08-24
      • 2015-01-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多