AngularJS自定义指令directive:父类scope和指令中scope之间的通信

我们知道angularJs中,指令中有scope,父类controller中也有scope,两者的通信方式有三中,分别是

  1. scope:false;直接使用父类的scope
  2. scope:true;继承父类的scope
  3. scope:{@,=,&}隔离的scope

但是1和2两种都有一定的缺陷,(1)直接使用父类,也就是在指令中也可以任意修父类的对象,这样使得父类对象被各种指令所修改。(2)继承父类,如果是对象传递到指令中,则可以直接造成父类的对象也修改,但是如果直接只是一个字符串,则父类的不会被修改。这个虽然比1种情况好一点,但是对于一个指令被用于多个位置的时候,项目中希望的是有些对象被修改,有些对象不会被修改。怎么样才能达到修改自如的情况,一般使用的是3种情况的scope,即隔离的scope。那么第3种情况,怎么实现传递字符串和传递对象。

1.最简单的scope

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>demo22.html</title>

    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="this is my page">
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    <script type="text/javascript" src="js/jquery-1.4.3.js"></script>
    <script type="text/javascript" src="js/angular.min.js"></script>
    <!--<link rel="stylesheet" type="text/css" href="./styles.css">-->

  </head>

  <body ng-app="app">
    <div ng-controller="controller"> 
        hello:{{name}}
        <!--<hello body="name"></hello>
    --></div>
    <script>
        var app = angular.module("app", []);
        app.controller("controller", function($scope){
            $scope.name = "hello";
        });
    </script>
  </body>
</html>

结果是hello

2.带指令的传递字符串的scope用的是@

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>demo22.html</title>

    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="this is my page">
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    <script type="text/javascript" src="js/jquery-1.4.3.js"></script>
    <script type="text/javascript" src="js/angular.min.js"></script>
    <!--<link rel="stylesheet" type="text/css" href="./styles.css">-->

  </head>

  <body ng-app="app">
    <div ng-controller="controller"> 
        hello:{{name}}
        <hello body="{{name}}"></hello>
    </div>
    <script>
        var app = angular.module("app", []);
        app.controller("controller", function($scope){
            $scope.name = "hello";
        });
        app.directive("hello", function(){
            return{
                restrict:"ECMA",
                template:'<div>指令中的name: {{body}}</div>',
                scope:{
                    body:'@'
                },

            }
        })
    </script>
  </body>
</html>

结果是:
hello:hello
指令中的name:hello

3.在指令中修改字符串

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>demo22.html</title>

    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="this is my page">
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    <script type="text/javascript" src="js/jquery-1.4.3.js"></script>
    <script type="text/javascript" src="js/angular.min.js"></script>
    <!--<link rel="stylesheet" type="text/css" href="./styles.css">-->

  </head>

  <body ng-app="app">
    <div ng-controller="controller"> 
        hello:{{name}}
        <hello body="{{name}}"></hello>
    </div>
    <script>
        var app = angular.module("app", []);
        app.controller("controller", function($scope){
            $scope.name = "hello";
        });
        app.directive("hello", function(){
            return{
                restrict:"ECMA",
                template:'<div>指令中的name: {{body}}</div>',
                scope:{
                    body:'@'
                },
                link : function(scope, elements, attrs, controller){ 
                    console.log(scope.body);
                    scope.body = "bbb";
                } 
            }
        })
    </script>
  </body>
</html>

结果是:
hello:hello
指令中的name:hello
结果还是一样的,说明@仅仅知识字符串的传递,在指令中修改scope不会对父类的scope字符串产生影响。

4.带指令的传递对象的scope用的是=

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>demo22.html</title>

    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="this is my page">
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    <script type="text/javascript" src="js/jquery-1.4.3.js"></script>
    <script type="text/javascript" src="js/angular.min.js"></script>
    <!--<link rel="stylesheet" type="text/css" href="./styles.css">-->

  </head>

  <body ng-app="app">
    <div ng-controller="controller"> 
        hello:{{name.age}}
        <hello body="name"></hello>
    </div>
    <script>
        var app = angular.module("app", []);
        app.controller("controller", function($scope){
            $scope.name = {age:"hello"};
        });
        app.directive("hello", function(){
            return{
                restrict:"ECMA",
                template:'<div>指令中的name: {{body.age}}</div>',
                scope:{
                    body:'='
                },

            }
        })
    </script>
  </body>
</html>

结果是:
hello:hello
指令中的name:hello
5.指令中修改对象

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>demo22.html</title>

    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="this is my page">
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    <script type="text/javascript" src="js/jquery-1.4.3.js"></script>
    <script type="text/javascript" src="js/angular.min.js"></script>
    <!--<link rel="stylesheet" type="text/css" href="./styles.css">-->

  </head>

  <body ng-app="app">
    <div ng-controller="controller"> 
        hello:{{name.age}}
        <hello body="name"></hello>
    </div>
    <script>
        var app = angular.module("app", []);
        app.controller("controller", function($scope){
            $scope.name = {age:"hello"};
        });
        app.directive("hello", function(){
            return{
                restrict:"ECMA",
                template:'<div>指令中的name: {{body.age}}</div>',
                scope:{
                    body:'='
                },
                link : function(scope, elements, attrs, controller){ 
                    console.log(scope.body);
                    scope.body.age = "bbb";
                } 
            }
        })
    </script>
  </body>
</html>

结果是:
hello:bbb
指令中的name:bbb。

 总结:
 如果选择隔离的scope,对上述的用法理解了,基本上你也知道怎么使用隔离的scope了。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值