angular.js - angular-translate 有条件输出词条有什么好办法?
漂亮男人
漂亮男人 2017-05-15 16:49:10
0
4
687

最近公司产品要做多语言支持,用了 angular-translate 来做,基本上还算顺利。

然后就碰到有的模板里是这样写的:

{{gender ? '男' : '女'}}

很显然,filter 没法直接用了,目前是用 ngIf 把它改写了,类似这样:

<span ng-if="gender">{{'common.male' | translate}}</span>
<span ng-if="!gender">{{'common.female' | translate}}</span>

但是如果不想要多余的 span 呢?有时候就是挺烦 Angular 这一点,为了一些指令你不得不整一些废标签出来。大家有什么想法?

漂亮男人
漂亮男人

reply all(4)
淡淡烟草味
<span>{{ (gender?'common.male':'common.female') | translate}}</span>
黄舟

The poster may try to use a controller method to implement it, such as:

<!-- template file -->
<span ng-bind="showGender(gender)"></span>
// angular controller
app.controller('MyCtrl', function ($scope, $filter)) {
    $scope.showGender = function (gender) {
        return $filter('filter_name')(gender);
    }
};

In this way, although the redundancy in html is reduced, the amount of code increases.

为情所困

The person who wrote this must be the principal!

Peter_Zhu

So profound

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template