test.html 3.96 KB
<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8" />
    <title></title>
    <style>
        .container>div {
            margin-bottom: 10px;
        }
    </style>
</head>

<body>
    <div ng-app="esNgAntd" ng-controller="mainCtrl">
        <div class="container" style="padding: 50px">
            <test a="123" b="{{b}}" c="{{c}}" d="{{d}}" e="{{e}}" f="1.00"></test>
        </div>
    </div>
    <script src="https://cdn.staticfile.org/angular.js/1.2.28/angular.min.js"></script>
    <script>
        angular
            .module("esNgAntd", [])
            .directive("test", function ($parse, $interpolate) {
                return {
                    replace: true,
                    restrict: "E",
                    scope: {
                        f: "="
                    },
                    template: `<div><h1>I am test{{foo}}{{c.a}}</h1></div>`,
                    compile: function (tElement, tAttrs, transclude) {
                        let props = {
                            a: {
                                type: "string",
                                compile: tAttrs.a.indexOf("{{") !== -1
                            },
                            b: {
                                type: "boolean",
                                compile: tAttrs.b.indexOf("{{") !== -1
                            },
                            c: {
                                type: "object",
                                compile: tAttrs.c.indexOf("{{") !== -1
                            },
                            d: {
                                type: "array",
                                compile: tAttrs.d.indexOf("{{") !== -1
                            },
                            e: {
                                type: "number",
                                compile: tAttrs.e.indexOf("{{") !== -1
                            }
                        }
                        return function ($scope) {
                            Object.keys(props).forEach(function (propKey) {
                                tAttrs.$observe(propKey, function (newVal) {
                                    let { type, compile } = props[propKey];
                                    if (!compile) {
                                        $scope[propKey] = newVal;
                                    } else if (type === "boolean") {
                                        $scope[propKey] = newVal === "true";
                                    } else if (["object", "array"].includes(type)) {
                                        $scope[propKey] = $parse(newVal)();
                                    } else if (type === "number") {
                                        let segments = newVal.split(".");
                                        let len = !segments[1] ? 0 : segments[1].length;
                                        $scope[propKey] = Math.floor(newVal * Math.pow(10, len)) / Math.pow(10, len);
                                    } else {
                                        $scope[propKey] = newVal;
                                    }
                                })
                            })
                            if (typeof $scope.constructor === "function") {
                                $scope.constructor();
                            }
                        }
                    },
                    controller: function ($scope, $element, $attrs) {
                        $scope.constructor = function () {
                            console.log($scope);
                        }
                    },
                }
            })
            .controller("mainCtrl", function ($scope, $timeout) {
                $scope.b = true;
                $scope.c = { a: 1 };
                $scope.d = ["a"];
                $scope.e = "1.0002";

                $timeout(function () {
                    $scope.foo = false;
                    $scope.c.a = 2;
                }, 3000)
            });
    </script>
</body>

</html>