How to call one filter from another filter in angular.js

后端 未结 5 1600
傲寒
傲寒 2020-12-14 14:57

I have a filter, linkifyStuff, in which I want some variables processed using another filter. I can\'t figure out the syntax to call one filter from another.

I know

5条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-14 15:08

    @useless asked if there was "any way to do this for default filters."

    A round-about way to use default filters: pipe it through the standard filter before passing it to your custom filter and pass the original value as a parameter.

    For example your Angular expression would look like this:

    {{myDate | date: 'mediumDate' | relativeDate: myDate}}
    

    And your filter:

    var myApp = angular.module('myApp',[]);
    myApp
        .filter('relativeDate', function() {
            return function(formattedDate, dateStr) {
                var returnVal = formattedDate,
                    today = new Date(),
                    evalDate = new Date(dateStr);
                if (today.getFullYear() == evalDate.getFullYear() &&
                    today.getMonth() == evalDate.getMonth() &&
                    today.getDate() == evalDate.getDate()) {
                        returnVal = 'Today'
                }
                return returnVal
            }
        })
        .controller('myAppCtrl', ['$scope', function myAppCntrl($scope) {
            $scope.myDate = "2001-01-01T01:01:01";
        });
    

提交回复
热议问题