0

I am trying to create a directive that will generate the current year for copyright. How do I access the year variable in the link of the directive? I've tried everything, but nothing works.

.directive('copyrightYear', function() {
    return {
        restrict: 'E',
        link: function(scope, element) {
            var date = new Date();
            var year = date.getFullYear();
        },
        template: ''
    };
})
3
  • Are you trying to access year from the template for the directive? Commented Jul 11, 2015 at 17:55
  • that looks like it would generate a year variable, but you don't appear to be using it anywhere. Commented Jul 11, 2015 at 17:56
  • @Travis Yes, I tried {{ year }} and {{ copyRight.year }}. Year returns nothing, copyright.year returns NaN. Commented Jul 11, 2015 at 19:55

1 Answer 1

3

Add it to the scope

.directive('copyrightYear', function() {
    return {
        restrict: 'E',
        link: function(scope, element) {
            var date = new Date();
            scope.year = date.getFullYear();
        },
        template: '{{year}}'
    };
});
Sign up to request clarification or add additional context in comments.

1 Comment

I was going to answer the same. ^1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.