RadioGroup.js 1.86 KB
import template from "./RadioGroup.html";
angular.module("esNgAntd").directive("antdRadioGroup", function () {
    return {
        controllerAs: "antdRadioGroup",
        restrict: "E",
        transclude: true,
        replace: true,
        scope: {
            value: "@",
            defaultValue: "@",
            onChange: "&",
        },
        template: template,
        controller: function ($scope, $element, $attrs) {
            this.getContext = function () {
                return $scope;
            };

            $scope.state = {
                value: $scope.value || $scope.defaultValue,
                childrens: [],
            };
            $scope.watch = {
                value: function (newVal) {
                    if (newVal !== undefined) {
                        $scope.state.value = newVal;
                        $scope.updateChildChecked();
                    }
                },
            };

            for (const key in $scope.watch) {
                $scope.$watch(key, $scope.watch[key], true);
            }

            $scope.updateChildChecked = function () {
                $scope.state.childrens.map(function (item) {
                    item.state.checked = $scope.state.value === item.value;
                });
            };

            $scope.setValue = function (event) {
                $scope.state.value = event.target.value;
                $scope.updateChildChecked();
                $scope.onChange({
                    event: event,
                });
            };
        },
        link: function ($scope, $element, $attrs, $controllers, $transclude) {
            for (let i = 0; i < $element[0].childNodes.length; i++) {
                let node = $element[0].childNodes[i];

                if (node.nodeType === 3) {
                    $element[0].removeChild(node);
                }
            }
        },
    };
});