Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs 角度平移在控制器中的应用_Angularjs - Fatal编程技术网

Angularjs 角度平移在控制器中的应用

Angularjs 角度平移在控制器中的应用,angularjs,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-translat

我正在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);
    };
}
问题:

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']没有返回任何信息。为什么?


如何解决这个问题?

如果要翻译多个字符串,应该使用数组。i、 e

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