简体   繁体   中英

Angularjs - how to access parent scope's variable by expression from a directive

Let's say I have a custom directive:

<my-directive attr="obj.property"></my-directive

Where obj.property means $scope.obj.property from the parent scope.

My directive js:

return {
        restrict:'A',
        link:function(scope,elem,attrs){

            //scope[attrs.attr] won't work

        }
    };

Inside the link function, how do I access $scope.obj.proprety by the expression passed from attr ?

scope[attrs.attr] won't work since it becomes scope['obj.property'] . I want to get scope.obj.property out of attrs.attr .

Any idea? Or is there any other way to achieve my goal here? Note that it can't be isolated scope.

只是:$ parse(attrs.attr)(范围)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM