Angular-translate在控制器中使用

时间:2016-03-02 18:45:52

标签: angularjs

我在AngularJS应用程序中使用角度转换为i18n。

代码:

angular
    .module('Test')
    .controller('AlertsCtrl', ['$translate', '$scope', AlertsCtrl]);

function AlertsCtrl($translate, $scope) {
    // Api: http://angular-translate.github.io/docs/#/guide/03_using-translate-service
    $translate('ALERT_MSG_1', 'ALERT_MSG_2').then(function (line) {
        $scope.alerts = [{
            type: 'success',
            msg: line['ALERT_MSG_1'] // Dosn't work
        }, {
            type: 'danger',
            msg: line['ALERT_MSG_2'] // Dosn't work
        }];
        console.log("In");
    });

    console.log("--- " + $translate.instant('ALERT_MSG_2')); // Works

    $scope.addAlert = function () {
        $scope.alerts.push({
            msg: 'Another alert!'
        });
    };

    $scope.closeAlert = function (index) {
        $scope.alerts.splice(index, 1);
    };
}

问题:

行['ALERT_MSG_1'] 行['ALERT_MSG_2'] dosnt会返回任何内容。为什么呢?

如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

你应该使用你想要翻译多个字符串的数组。即。

$translate(['ALERT_MSG_1', 'ALERT_MSG_2']).then(function (line) {
    $scope.alerts = [{
        type: 'success',
        msg: line['ALERT_MSG_1'] // Dosn't work
    }, {
        type: 'danger',
        msg: line['ALERT_MSG_2'] // Dosn't work
    }];
    console.log("In");
});