angularjs : logging scope property in directive link function displays undefined

前端 未结 3 2350
温柔的废话
温柔的废话 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:54

    Pawel is right (https://stackoverflow.com/a/14552200/287070) but I wanted to add that the problem is that any attribute that contains {{}} interpolation will be set to null in the attrs parameter during the link function as the first $digest since the compilation has not yet run to evaluate these.

    The fact that @ bindings are null in linking functions is just a symptom of this.

    Currently there is no real fix, since we can't start running $digests in the middle of the compilation process. So $observe (or $watch) is the only real way to get hold of these values.

提交回复
热议问题