angular js 自定义指令

我们有些时候需要把后台返回过来的带有html标签的字符串binding到界面中一个指定的div或者其他的控制器中。

使用普通ng-bind不会自动解析出html语句。

js中这样定义:

app.directive('dynamic', function ($compile) {
    return {
        restrict: 'A',
        replace: true,
         link: function (scope, ele, attrs) {
        scope.$watch(attrs.dynamic, function(html) {
            ele.html(html);
            $compile(ele.contents())(scope);
       });
     }
   };
});

html中绑定指令

<p class="before_over" dynamic="DetailsPageModule.content">

</p>

原文地址:https://www.cnblogs.com/xiezhonglong/p/5760071.html