angular-translate ad ng-options

前端 未结 1 1377
攒了一身酷
攒了一身酷 2021-01-11 23:54

I\'m trying to translate a select drop-down and I don\'t like the way I\'m doing it, because it\'s cumbersome and it bypasses the whole angular-translate framework.

相关标签:
1条回答
  • 2021-01-12 00:44

    I didn't completely get what you're trying to achieve, but I'll put some code that works fine for reloading the options translations with the | translate filter.

    Assuming you have this json as key/values for your translations:

    var english = {"lang": {
                     "label": "text", 
                     "select": {
                        "k1": "var1", 
                        "k2": "var2"
                     }}
                  };
    

    And your controller creates a list of options like this:

    $scope.optionsList = [
        {val: 'var1', translationKey: 'lang.select.k1'},
        {val: 'var2', translationKey: 'lang.select.k2'}
    ];
    

    You should be good to go inserting the translate filter after the option label in the ng-options expression:

    <select ng-model="selectedOpt" 
            ng-options="opt.val as opt.translationKey | translate for opt in optionsList">
    </select>
    

    Hope it helps!

    0 讨论(0)
提交回复
热议问题