0

I have Angular directive for attribute title.

.directive('titleDate', function () {
    return {
        restrict: 'A',
        scope: {
            datetime: '=titleDate'
        },

        link: function($scope, element) {
            element.attr('title','{{ '+$scope.datetime+' | amDateFormat: \'D MMMM HH:mm\' }}');             
        }
    }
});

Directive add title for component. But title return string expression like:

{{ 21.04.2014:15.20.22 | amDateFormat: 'D MMMM HH:mm' }}

So filter not apply. How compile this string or that i'm doing wrong?

2 Answers 2

1

A better way to use filter in a directive is to inject $filter service:

$filter('amDateFormat')($scope.datetime, 'D MMMM HH:mm');

So your code would look like:

.directive('titleDate', ['$filter', function () {
    return {
        restrict: 'A',
        scope: {
            datetime: '=titleDate'
        },

        link: function($scope, element) {
            element.attr('title', $filter('amDateFormat')($scope.datetime, 'D MMMM HH:mm'));             
        }
    }]
});
Sign up to request clarification or add additional context in comments.

Comments

0

the date 21.04.2014:15.20.22 should be inside quotes in the expression like '21.04.2014:15.20.22'

2 Comments

element.attr('title','{{ \''+$scope.datetime+'\' | amDateFormat: \'D MMMM HH:mm\' }}'); not help!
I would guess that in the titleDate attribute of the directive you would want @ (one-way) binding just to get a value there (<div title-date="{{2 | currency}}"></div>) (I used the currency filter because i don't have moment library available) and then in the directive you should have elem.attr('title', $scope.datetime). it should then bind correct

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.