• angular : direative : scope | 指令scope里的符号@,=


     先看看以下的代码

    <body ng-app="app" ng-controller="ctrl">
        <dir myname="name"></dir>
        <script src="js/angular.js"></script>
        <script>
            var app = angular.module("app", []);
            app.controller("ctrl", function ($scope, $timeout) {
                $scope.name = "ABC";
                $timeout(function () {
                    $scope.name = "XYZ";
                    //console.log("ctrl :" + $scope.name);
                }, 2000);
            });
            app.directive("dir", function ($timeout) {
                return {
                    restrict: "E",
                    template: "<div>{{dirName}}</div>",
                    link: function (scope, elem, attr) {
                        $timeout(function () {
                            //scope.name = "XYZ";
                            console.log("dir :" + scope.dirName);
                        }, 3000);
                    },
                    scope: {
                        dirName: "=myname"
                    }
                }
            });
        </script>
    </body>
    

    以上 ^ : 一开始会显示ABC,2秒后会显示XYZ。*外面可以同步内部

            app.controller("ctrl", function ($scope, $timeout) {
                $scope.name = "ABC";
                $timeout(function () {
                    //$scope.name = "XYZ";
                    //console.log("ctrl :" + $scope.name);
                }, 2000);
            });
            app.directive("dir", function ($timeout) {
                return {
                    restrict: "E",
                    template: "<div>{{dirName}}</div>",
                    link: function (scope, elem, attr) {
                        $timeout(function () {
                            scope.dirName = "XYZ";
                            console.log("dir :" + scope.dirName);
                        }, 3000);
                    },
                    scope: {
                        dirName: "=myname"
                    }
                }
            });
    

    以上^ : 开始显示ABC,3秒后显示XYZ。*内部可以同步外部

            app.controller("ctrl", function ($scope, $timeout) {
                $scope.name = "ABC";
                $timeout(function () {
                    $scope.name = "XYZ";
                    //console.log("ctrl :" + $scope.name);
                }, 2000);
            });
            app.directive("dir", function ($timeout) {
                return {
                    restrict: "E",
                    template: "<div>{{dirName}}</div>",
                    link: function (scope, elem, attr) {
                        $timeout(function () {
                            //scope.dirName = "XYZ";
                            console.log("dir :" + scope.dirName);
                        }, 3000);
                    },
                    scope: {
                        dirName: "@myname"
                    }
                }
            });
    

    以上^ : 开始显示name,3秒后显示name。*内部的符号“@”只能拿到string

    <body ng-app="app" ng-controller="ctrl">
        <dir myname="{{name}}"></dir>
        <script src="js/angular.js"></script>
        <script>
            var app = angular.module("app", []);
            app.controller("ctrl", function ($scope, $timeout) {
                $scope.name = "ABC";
                $timeout(function () {
                    $scope.name = "XYZ";
                    //console.log("ctrl :" + $scope.name);
                }, 2000);
            });
            app.directive("dir", function ($timeout) {
                return {
                    restrict: "E",
                    template: "<div>{{dirName}}</div>",
                    link: function (scope, elem, attr) {
                        $timeout(function () {
                            //scope.dirName = "XYZ";
                            console.log("dir :" + scope.dirName);
                        }, 3000);
                    },
                    scope: {
                        dirName: "@myname"
                    }
                }
            });
        </script>
    </body>
    

    以上 ^ : 一开始会显示ABC,2秒后会显示XYZ。*外面可以同步内部,同时注意div里是使用{{}}的表达式

            app.controller("ctrl", function ($scope, $timeout) {
                $scope.name = "ABC";
                $timeout(function () {
                    //$scope.name = "XYZ";
                    //console.log("ctrl :" + $scope.name);
                }, 2000);
            });
            app.directive("dir", function ($timeout) {
                return {
                    restrict: "E",
                    template: "<div>{{dirName}}</div>",
                    link: function (scope, elem, attr) {
                        $timeout(function () {
                            scope.dirName = "XYZ";
                            console.log("dir :" + scope.dirName);
                        }, 3000);
                    },
                    scope: {
                        dirName: "@myname"
                    }
                }
            });
    

    以上 ^ : 一开始会显示ABC,2秒后会显示XYZ。*内面可以同步外部,同时注意div里是使用{{}}的表达式

  • 相关阅读:
    2019.01.06-dtoj-2437: [Noi2011]兔兔与蛋蛋
    2019.01.03-bzoj-1443-[JSOI2009]游戏Game
    2019.01.03-dtoj-4104-yjqaa
    2019.01.03-sg函数模板
    2019.01.02-bzoj-4550: 小奇的博弈
    2019.01.02-dtoj2293-幻想乡开店(shop)
    OCP 071中文考试题库(cuug内部资料)第5题
    OCP 063中文考试题库(cuug内部资料)第4题
    OCP 063中文考试题库(cuug内部资料)第3题
    OCP 062中文考试题库(cuug内部资料)第4题
  • 原文地址:https://www.cnblogs.com/stooges/p/3956112.html
Copyright © 2020-2023  润新知