How to display placeholders in AngularJS for undefined expression values?

后端 未结 5 1683
情歌与酒
情歌与酒 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:58

    Implement default filter:

    app.filter('default', function(){
      return function(value, def) {
        return (value === undefined || value === null? def : value);
      };
    });
    

    And use it as:

    {{ x | default: '?' }}
    

    The advantage of the filter solution over {{ x || '?'}} is that you can distinguish between undefined, null or 0.

提交回复
热议问题