2016-03-02 25 views
0

我在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給回任何東西。爲什麼?

這怎麼解決?

回答

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"); 
});