Angular: direative: Scope | symbol in the Command scope @, =

Source: Internet
Author: User

Let's take a look at the following code:

<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>

^ Above: ABC is displayed at the beginning, and XYZ is displayed in 2 seconds. * Internal synchronization is allowed.

 

        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"                }            }        });

^ Above: ABC is displayed, and XYZ is displayed 3 seconds later. * External data can be synchronized internally.

 

        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"                }            }        });

^ Above: Name is displayed, and name is displayed 3 seconds later. * The internal symbol "@" can only get the 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>

^ Above: ABC is displayed at the beginning, and XYZ is displayed in 2 seconds. * Internal synchronization can be performed on the External. Note that the DIV uses the {} expression.

 

        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"                }            }        });

^ Above: ABC is displayed at the beginning, and XYZ is displayed in 2 seconds. * The internal area can be synchronized with the external area. Note that the DIV contains the {} expression.

Angular: direative: Scope | symbol in the Command scope @, =

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.