简体   繁体   中英

directive link attrs don't work

in html i call my directive with: <div godata-pagination count="{{pagination.count}}" size="{{pagination.size}}" page="{{pagination.page}}"></div>

then my directive looks inside:

function link(scope, element, attrs) {
    console.log('attrs: ' + attrs.size);
    scope.pagCount = attrs.count;
    scope.pagPage = attrs.page;
}
return {
    restrict: 'A',
    transclude: true,
    scope: {
        count: '@',
        size: '@',
        page: '@'
    },
    templateUrl: 'partials/godata/pagination.html',
    link: link
};

In the partial (partials/godata/pagination.html) i can access these variables (count, size and page). For 100% in partial i access originaly these three variables, no other in this scope.

But i will do something with these variables in function link. If i try to access these variables in function link through attrs, they are empty :(

What is wrong and/or what can i do?

UPDATE:

控制台日志输出

And with scope: false scope variable is undefined.

And why brings the line attrs.$observe('page', function(val) { console.log('$observe: ' + val); }); in function link two output ...one with and one without value?

UPDATE again:

function link(scope, element, attrs) {
    console.log('scope: ' + scope.size);
    console.log('scope: ' + scope);
    console.log('attrs: ' + attrs.size);
    console.log('attrs: ' + attrs);
    attrs.$observe('size', function(size) {
        console.log('$observe: ' + size);
        if(size) {
            scope.pagSize = size;
        }
    });
    console.log('after observe; pagSize: ' + scope.pagSize);
}

...bring me:

控制台日志输出

But scope.pagSize has a value in browser.

How can i work with these attributes in my directive, if i can't access them?!

They are bound to the scope of the directive.

You can access them with scope.page , scope.count for example.

As @Hristo Enev Answered : when you have isolated scope it is better to access attrs with scope.page or scope.count

but if you insist to do that with attribute,

this is the way:

attrs.$observe('page', function(val) { console.log(val) })

results in the value of page attribute.

Inside the linking function, you should use $observe() to get interpolated value.

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