How to display placeholders in AngularJS for undefined expression values?

后端 未结 5 1689
情歌与酒
情歌与酒 2021-02-02 05:13

If I have an expression {{ x }} and x is undefined or null, then how can I display a placeholder for it?

I provided one solution

5条回答
  •  栀梦
    栀梦 (楼主)
    2021-02-02 05:42

    I would do it like this, but maybe there is a better way:

    angular.module('app').filter('placeholdEmpty', function(){
      return function(input){
        if(!(input == undefined || input == null)){
          return input;
        } else {
          return "placeholder";
        }
      }
    });
    

    and then use {{ x | placeholdEmpty}}

提交回复
热议问题