gpt4 book ai didi

javascript - '$translate.uses(key)' undefined 不是 AngularJS 中的函数

转载 作者:行者123 更新时间:2023-11-29 16:56:37 41 4
gpt4 key购买 nike

我正在尝试在我的应用程序中实现 Angular 转换。所以,我找到了以下 JSFiddle这应该演示 Angular 翻译,但我收到以下错误消息:

'undefined' is not a function (evaluating '$translate.uses(key)')

var myApp = angular.module('myApp', ['pascalprecht.translate']);

myApp.config(function($translateProvider) {
$translateProvider.preferredLanguage('fr');
$translateProvider.translations('en', {
MONDAY: 'Monday',
TUESDAY: 'Tuesday',
WEDNESDAY: 'Wednesday',
THURSDAY: 'Thursday',
FRIDAY: 'Friday',
SATURDAY: 'Saturday',
SUNDAY: 'Sunday'
});
$translateProvider.translations('fr', {
MONDAY: 'Lundi',
TUESDAY: 'Mardi',
WEDNESDAY: 'Mercredi',
THURSDAY: 'Jeudi',
FRIDAY: 'Vendredi',
SATURDAY: 'Samedi',
SUNDAY: 'Dimanche'
});
});

myApp.constant('weekDays', [{
id: 1,
key: 'MONDAY'
}, {
id: 2,
key: 'TUESDAY'
}, {
id: 3,
key: 'WEDNESDAY'
}, {
id: 4,
key: 'THURSDAY'
}, {
id: 5,
key: 'FRIDAY'
}, {
id: 6,
key: 'SATURDAY'
}, {
id: 7,
key: 'SUNDAY'
}]);

myApp.controller('MyCtrl', ['$scope', '$translate', 'weekDays',
function($scope, $translate, weekDays) {

$scope.changeLanguage = function(key) {
$translate.uses(key)
}
$scope.days = weekDays;
$scope.name = 'user';
console.log($scope.days);
}
]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.22/angular.min.js"></script>
<script src="http://rawgithub.com/PascalPrecht/bower-angular-translate/master/angular-translate.min.js"></script>

<body ng-app="myApp">
<div ng-controller="MyCtrl">
Hello, {{name}}!
<div>
<button ng-click="changeLanguage('fr')">fr</button>
<button ng-click="changeLanguage('en')">en</button>
</div>

<ul>
<li ng-repeat="day in days">
{{ day.key | translate }}
</li>
</ul>

</div>
</body>

知道为什么会这样吗?

最佳答案

$translate.uses(key) 已弃用,请改用 $translate.use(key)

阅读更多关于 angular-translate Multi Language 的信息和 2.0.0 upgrade notification (重大更改)。

var myApp = angular.module('myApp', ['pascalprecht.translate']);

myApp.config(function($translateProvider) {
$translateProvider.preferredLanguage('fr');
$translateProvider.translations('en', {
MONDAY: 'Monday',
TUESDAY: 'Tuesday',
WEDNESDAY: 'Wednesday',
THURSDAY: 'Thursday',
FRIDAY: 'Friday',
SATURDAY: 'Saturday',
SUNDAY: 'Sunday'
});
$translateProvider.translations('fr', {
MONDAY: 'Lundi',
TUESDAY: 'Mardi',
WEDNESDAY: 'Mercredi',
THURSDAY: 'Jeudi',
FRIDAY: 'Vendredi',
SATURDAY: 'Samedi',
SUNDAY: 'Dimanche'
});
});

myApp.constant('weekDays', [{
id: 1,
key: 'MONDAY'
}, {
id: 2,
key: 'TUESDAY'
}, {
id: 3,
key: 'WEDNESDAY'
}, {
id: 4,
key: 'THURSDAY'
}, {
id: 5,
key: 'FRIDAY'
}, {
id: 6,
key: 'SATURDAY'
}, {
id: 7,
key: 'SUNDAY'
}]);

myApp.controller('MyCtrl', ['$scope', '$translate', 'weekDays',
function($scope, $translate, weekDays) {

$scope.changeLanguage = function(key) {
$translate.use(key)
}
$scope.days = weekDays;
$scope.name = 'user';
console.log($scope.days);
}
]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.22/angular.min.js"></script>
<script src="http://rawgithub.com/PascalPrecht/bower-angular-translate/master/angular-translate.min.js"></script>

<body ng-app="myApp">
<div ng-controller="MyCtrl">
Hello, {{name}}!
<div>
<button ng-click="changeLanguage('fr')">fr</button>
<button ng-click="changeLanguage('en')">en</button>
</div>

<ul>
<li ng-repeat="day in days">
{{ day.key | translate }}
</li>
</ul>

</div>
</body>

关于javascript - '$translate.uses(key)' undefined 不是 AngularJS 中的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31926252/

41 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com