angularjs1继承问题

来源:互联网 发布:http协议的端口号 编辑:程序博客网 时间:2024/06/03 22:47
 angularjs代码:
<script>    (function () {        var root =  angular.module("root",["m1"]);               var m1 = angular.module("m1",[]);        m1.controller("m1One",function ($scope) {            $scope.msg = "hello m1 one"        });        m1.controller("m1Two",function ($scope) {            $scope.msg = "hello m1 Two";            $scope.pmsg = $scope.$parent.msg;()        });        m1.controller("m1Three",function ($scope) {            $scope.msg = "hello m1 Three";            $scope.pmsg = $scope.$parent.msg;            $scope.gmsg = $scope.$parent.$parent.msg;        })    })()</script>

html代码:
<div ng-controller="m1One" class="well">        {{msg}}        <div ng-controller="m1Two" class="well">            {{pmsg}} --{{msg}}            <div ng-controller="m1Three" class="well">                {{pmsg}} ----- {{msg}}                <hr>                {{gmsg}}            </div>        </div>    </div>
其运行结果为:


拓展:

$scope.$parent
本质:子域不继承父域,并建立独立作用域。
、当scope对象为空对象时,无论是父域parentName,还是指令子域parentName发生变更,都不会影响到对方。
原理很清楚,就是指令建立的独立作用域,与父域是完全隔离的。