AngularJs自定义指令中scope属性三种值的例子?

<div ng-init="siblingProperty='moredata'">

    Inside Div Two: {{ aThirdProperty }}
        <div ng-init="aThirdProperty = 'data for 3rd property'" ng-controller="SomeController">
            Inside Div Three: {{ aThirdProperty }}
            <div ng-controller="SecondController">
                Inside Div Four: {{ aThirdProperty }}
                <br>
                Outside myDirective: {{ myProperty }}
                <div my-directive ng-init="myProperty = 'wow, this is cool'" >
                    Inside myDirective: {{ myProperty }}
                <div>
            </div>
        </div>
    </div>
    <script>
        angular.module('myApp', [])
        .controller("SomeController",function(){
            
        })
        .controller("SecondController",function(){
            
        })
        .directive('myDirective', function() {
            return {
                restrict: 'A',
                scope: {}
            };
        });
    </script>

我的理解是:
ng-init="myProperty = 'wow, this is cool'"初始化内部作用域值。
scope为false时,指令和父级作用域共同使用一个作用域,所以Outside myDirective有值;
scope为true时,指令继承父级作用域并创建一个新作用域,所以初始化后,Outside myDirective在外部不能取到这个新创建作用域的值;
问题来了,scope为{}时,不是生成的隔离作用域吗,为什么外面Outside myDirective有值呢??

阅读 1.9k
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题