AngularJS - Scope value not accessible in the directive Scope

前端 未结 6 1465
梦谈多话
梦谈多话 2021-01-26 09:01

FIDDLE

I have created a directive :-

return {
        restrict: \'EAC\',
        scope: {
            statesActive: \'=\'
        },            
                 


        
6条回答
  •  南方客
    南方客 (楼主)
    2021-01-26 09:53

    A slight tweak to the fiddle was referenced in a comment to another answer that now shows the retrieval of states within the directive's link function via its scope:

    link: function (scope, element, attrs) {            
        var stateData = $parse(scope.statesActive)();
        console.log(stateData);
    }
    

    by stating:

    scope: { statesActive: '=' }
    

    you are binding your directive's scope to the attribute named states-active so all you have to do reference it inside the directive is to use

    scope.statesActive
    

    this will get the value as a string, you can use the $parse service to get the value of your object

提交回复
热议问题