最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - AngularJS directive transclude scope=false? - Stack Overflow

programmeradmin0浏览0评论

How to prevent directive with transclude to create new scopes?

This jsfiddle I cant bind anything due to the new scopes illustrated with red borders.

Html:

<div ng-app="ponents">
    <input ng-model="var">
    <block>
        123
        <input ng-model="var">
    </block>
</div>

JavaScript:

angular.module('ponents', []).directive('block',function(){
    return{
        scope:false,
        replace:true,
        restrict:"E",
        transclude:true,
        template:'<div class="block" ng-transclude></div>',
        link:function(scope, el, attrs, ctrl){

        }
    }
});

CSS:

.ng-scope{
  border:1px solid red;
    margin:10px;
}

How to prevent directive with transclude to create new scopes?

This jsfiddle I cant bind anything due to the new scopes illustrated with red borders.

Html:

<div ng-app="ponents">
    <input ng-model="var">
    <block>
        123
        <input ng-model="var">
    </block>
</div>

JavaScript:

angular.module('ponents', []).directive('block',function(){
    return{
        scope:false,
        replace:true,
        restrict:"E",
        transclude:true,
        template:'<div class="block" ng-transclude></div>',
        link:function(scope, el, attrs, ctrl){

        }
    }
});

CSS:

.ng-scope{
  border:1px solid red;
    margin:10px;
}
Share Improve this question edited Sep 10, 2017 at 17:48 Cœur 38.8k26 gold badges205 silver badges277 bronze badges asked Sep 27, 2012 at 19:59 JossiJossi 1,0801 gold badge18 silver badges28 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 7

It is actually expected behavior as stated here (ng-transclude create a child scope): https://github./angular/angular.js/issues/1056 and discussed here: https://groups.google./forum/#!msg/angular/45jNmQucSCE/hL8x48-JfZIJ

You can workaround this by setting a member on an object in the scope (obj.var) like in this fiddle: http://jsfiddle/rdarder/pnSNj/10/

Although this is not remended by the Angular team, another workaround is to explicitly call the $parent scope within the transcluded portion:

<div ng-app="ponents">
    <input ng-model="var">
    <block>
        123
        <input ng-model="$parent.var">
    </block>
</div>
发布评论

评论列表(0)

  1. 暂无评论