angularjs : logging scope property in directive link function displays undefined

前端 未结 3 2348
温柔的废话
温柔的废话 2021-02-20 17:30

I have this basic plnkr which just implements a basic \"Hello, X\" directive. In the link function I am logging scope.name but I get undefined? Why is

3条回答
  •  [愿得一人]
    2021-02-20 17:58

    This is a known "problem" where interpolation of @ attributes happens after linking function is invoked. There is a pull request open to change this issue but it is not clear if this one is going to be merged.

    In the meantime a way of getting an interpolated value is by observing an attribute like so:

    attrs.$observe('hello', function(changedValue){
         console.log(scope.name);
    });
    

    And the plunk: http://plnkr.co/edit/Lnw6LuadTLhhcOTsPC8w?p=preview

    So, at the end of the day this is a bit confusing behavior of AngularJS that might be changed in the future.

提交回复
热议问题