Use filter on ng-options to change the value displayed

前端 未结 3 860
栀梦
栀梦 2020-12-01 03:17

I have an array of prices (0, 0.99, 1.99... etc) that I want to display in

提交评论

  • 2020-12-01 04:08

    You want to create the custom filter such as:

    app.filter('price', function() {
      return function(arr) {
        return arr.map(function(num){
          return num === 0 ? 'free' : num + '$';
        });
      };
    });
    

    use it like:

    <select ng-model="create_price" ng-options="obj for obj in prices | price">
      {{ obj }}
    </select>
    
    0 讨论(0)
  • 2020-12-01 04:12

    Pardon the pseudo code

    data-ng-options="( obj.property | myFilter ) for obj in objects"
    
    app.filter('myFilter', function() {
      return function(displayValue) {
        return (should update displayValue) ? 'newDisplayValue' : displayValue;
    });
    
    0 讨论(0)
  • 提交回复
    热议问题