Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS directive transclude scope=false?

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="components">
    <input ng-model="var">
    <block>
        123
        <input ng-model="var">
    </block>
</div>

JavaScript:

angular.module('components', []).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;
}
like image 636
Jossi Avatar asked Sep 27 '12 19:09

Jossi


1 Answers

It is actually expected behavior as stated here (ng-transclude create a child scope): https://github.com/angular/angular.js/issues/1056 and discussed here: https://groups.google.com/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.net/rdarder/pnSNj/10/

like image 110
Guillaume86 Avatar answered Sep 25 '22 19:09

Guillaume86