angularjs: multiple values in a ng-switch-when

前端 未结 5 1878
不知归路
不知归路 2021-02-11 12:03

I have the following ngSwitch:

Wrong

5条回答
  •  [愿得一人]
    2021-02-11 12:28

    You can add a filter to the status that maps values that mean the same thing into the same value.

    .filter('meaning', function() {
        return function(input) {
          input = input || '';
          if (['wrong', 'amiss','awry', 'bad', 'erroneous', 'false', 'inaccurate',\
               'misguided', 'mistaken', 'unsound', 'incorrect'].indexOf(input) != -1)
              return 'wrong';
          // You can make it generic like this:
          synonymsDictionary = {
            'someWord' : ['syn1', 'syn2', 'syn3' ... ],
            'someOtherWord' : ['otherWordSyn1', 'otherWordSyn2', 'otherWordSyn3' ...]
            .
            .
            .
          };
    
          for (var word in synonymsDictionary)
              if (synonymsDictionary[word].indexOf(input) != -1)
                  return word; // This way you could iterate over a bunch of arrays...
    
             // Edge case
             else return input;
        };
      })
    

    Then you simply

    Wrong Correct

    Although in your case, you may have just wanted to print a message so you could have pulled the message from a dictionary...

    Something like:

    
        {{getStatusMessage(status)}}
    
    

提交回复
热议问题