AngularJS - Access isolated scope in directive's link function

前端 未结 4 2112
北海茫月
北海茫月 2020-12-13 09:46

I\'m giving a first try at AngularJS custom directives.

I\'m having trouble using (or understanding ...) the isolated scope in the link function of the directive.

4条回答
  •  萌比男神i
    2020-12-13 09:58

    well, none of the answers above actually mentioned one key aspect, even with '=', it doesn't seem to me you can access the scope inside link function directly like the following,

    scope: {
        data: '=',
    },
    link: function(scope, elem, attrs) {
        console.debug(scope.data); // undefined
    

    but you can access the scope in the internal function,

    link: function(scope, elem, attrs) {
        scope.saveComment = function() {
            console.debug(scope.data);
    

    so it seems to me there might be a time lag in when scope.data can be available.

提交回复
热议问题