How do I combine an if/else expression with angular translate inside an attribute?

Felix picture Felix · Feb 11, 2016 · Viewed 7k times · Source

I'm using an if/else expression and a translation of the possible values inside the placeholder-Tag of an HTML input-element. It obviously doesn't work this way, because of the nested double quotes inside the placeholder-tag:

<input type="number" 
       placeholder="{{constraint ? '{{"TERM_A" | translate}}' : '{{"TERM_B" | translate}}'}}"
       ng-model="" 
       required 
       autocapitalize="none" 
       autocorrect="off" /> 

How do I set the single/double-quotes accordingly or is there even a more elegant solution?

Answer

Outside_Box picture Outside_Box · Dec 20, 2017

Proper way:

 <input type="number" 
               placeholder="{{ (constraint ? 'TERM_A' : 'TERM_B') | translate }}"
               ng-model="" 
               required 
               autocapitalize="none" 
               autocorrect="off" /> 

Another sample:

            label="{{ (detailsTriggered ? 'ui.showDetails' : 'ui.hideDetails') | translate}}"

Beware of " [ ] " braces, types of quotation marks and apostrophes.