javascript - angular指令的隔离作用域问题
高洛峰
高洛峰 2017-04-11 11:40:47
[JavaScript讨论组]
 **指令中的scope:{}时不是会创建隔离作用域,指令隔离作用域内不能访问父作用域的属性,然而这个竟然能,望高手指点一二,刚上手angular-----**
 

Surrounding scope: {{ myProperty }}

Inside an directive with inherited scope: {{ myProperty }}

Inside myDirective, isolate scope: {{ myProperty }} **//这里的myProperty竟然也能访问父作用域的myProperty**

高洛峰
高洛峰

拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...

全部回复(1)
迷茫

你应该这么写:

//当然html中就不需要写template中的部分了。
.directive('myDirective', function() {
      return {
        restrict: 'A',
        scope: {},
        template:"Inside myDirective, isolate scope: {{ myProperty }}"
      };
    })

按照你的写法,添加了指令的元素并没有按照你的设计被编译。
或者干脆这么写

.directive('mysss', function() {
        return {
            restrict: 'A',
            scope: {},
            replace : true,
            template:"<p>Inside an directive with inherited scope: {{ myProperty }}</p>"
        };
    })
//replace:true时,添加了指令的元素会被template内内容整体替换。
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号