美文网首页
2020-09-24 angularjs 删除ng-transc

2020-09-24 angularjs 删除ng-transc

作者: rub1cky | 来源:发表于2020-09-24 10:41 被阅读0次

    自定义 ng-transclude-replace 指令

    directive('ngTranscludeReplace', ['$log', function ($log) {
        return {
            terminal: true,
            restrict: 'EA',
            link: function ($scope, $element, $attr, ctrl, transclude) {
                if (!transclude) {
                    $log.error('orphan',
                        'Illegal use of ngTranscludeReplace directive in the template! ' +
                        'No parent directive that requires a transclusion found. ');
                    return;
                }
                transclude(function (clone) {
                    if (clone.length) {
                        $element.replaceWith(clone);
                    } else {
                        $element.remove();
                    }
                });
            }
        };
    }]);
    

    原来ng-transclude生成的dom结构

    <div>
        <span ng-transclude>
            ......
        </span>
    </div>
    

    替换之后的结构

    <div>
        ......
    </div>
    

    相关文章

      网友评论

          本文标题:2020-09-24 angularjs 删除ng-transc

          本文链接:https://www.haomeiwen.com/subject/qydzyktx.html