why angular.js transclude child childScope not inh

2019-07-25 16:55发布

Thank you very much concerned about this issue, this problem is angular child scope not inherited from the parent scope when I use the transclude directive, this issue has a relationship with angular.js versions before 1.2.17 can work release, version 1.2.18 after are ineffective

These are two versions of the actual test results. http://jsfiddle.net/a3ywb2c4/

var App= angular.module("myApp",[]);
App.controller("testCtrl",function($scope){
$scope.names= [{name:"janry",value:123},{name:"janry",value:123},{name:"janry",value:123}]
$scope.dd="hello world"

});

App.directive("testDire",function($parse){
return {
    restrict:'A',
    transclude:true,
    template:"<div  ng-repeat='$item in $items' ng-transclude></div>",
    scope:false,
    link:function(scope,iele,attrs,ctrl,transclude){
         var getter=$parse(attrs.data)
         scope.$items = getter(scope);     
    }
};

})

http://jsfiddle.net/m3fo9om3/

1条回答
叛逆
2楼-- · 2019-07-25 17:29

This is how AngularJS is built: the scope for the transcluded directive is a sibling of the container directive, and they both inherit the parent scope.

This post has very good explanations and work arounds.

查看更多
登录 后发表回答