Slot.js 1.2 KB
angular.module("esNgAntd").directive("esSlot", function ($compile) {
    return {
        controllerAs: "esSlot",
        restrict: "E",
        transclude: true,
        replace: true,
        scope: {
            content: "@",
            context: "=",
        },
        controller: function ($scope, $element, $attrs) {
            this.getContext = function () {
                return $scope;
            };

            $scope.watch = {
                content: (newValue) => {
                    if (newValue !== undefined) {
                        if (/<[^>]+>/.test(newValue)) {
                            $element.replaceWith(
                                $compile(newValue)(
                                    $scope.context ? $scope.context : $scope
                                )
                            );
                        } else {
                            $element.text(newValue);
                        }
                    }
                },
            };

            for (const key in $scope.watch) {
                $scope.$watch(key, $scope.watch[key], true);
            }
        },
        link: function ($scope, $element, $attrs, $controllers, $transclude) {},
    };
});