angularjs 动态加载指令------编译服务$compile

时间:2023-03-10 05:35:12
angularjs 动态加载指令------编译服务$compile

场景:

我们写了一个自定义的指令,这条指令需要一些数据,而这些数据需要在某些操作之后才能就绪,这时候,我们就需要在数据就绪之后,动态加载指令。

示例:

  js:

$scope.$watch('report.reportTime', function (newVal, oldVal) {
            if (newVal !== oldVal) {
                $("#test").html($compile("<select-users hasDefalut='true' isDefaultCanDelete='true' selected-users='selectedUsers'>")($scope));
            }
        });

html:

   <div id="test">   </div>

注意:

$compile 为angular的编译函数,使用时需要注入到controller中,

使用方法:$compile(ele)($scope)