我想在我的Angular項目中使用i18next(https://github.com/archer96/ng-i18next),但它似乎加載速度太慢。這是我的設置:如何等待Angular模塊加載?
angular.module('jm.i18next').config(['$i18nextProvider', function ($i18nextProvider) {
$i18nextProvider.options = {
lng: 'en',
fallbackLng: 'en',
preload: ['en'],
supportedLngs: ['en'],
resGetPath: '../locales/__lng__.json',
useCookie: false,
useLocalStorage: false,
defaultLoadingValue: ''
};
}]);
angular.module('myApp', ['jm.i18next']).controller('MainCtrl', ['$scope', '$i18next', function ($scope, $i18next) {
console.log($i18next("key"));
setTimeout(function() {
console.log($i18next("key"));
}, 1000);
}]);
我必須添加一個超時纔能有一個值。有沒有辦法確保控制器加載後i18next已準備就緒?
UPDATE
我的類型,這是使用$ i18next翻譯試圖組訓練。但是這不起作用,因爲在控制器完成翻譯之前視圖已經「準備好」了。
<select ng-model="workout" ng-options="workout as workout.name group by workout.type for workout in workouts | orderBy: ['type', 'name']"></select>
$scope.workouts = [
{id: 1, name: 'Workout 1', type: $i18next("type1")},
{id: 25, name: 'Workout 2', type: $i18next("type2")},
];
嘿,是不是你的控制器鏈接到一個視圖?如果是這樣,您可以使用路徑上的解決方案來等待模塊加載。但通常角將等待,直到所有模塊加載之前運行...該模塊中是否有一個Ajax請求? – 2014-10-28 13:07:57
它鏈接到一個視圖,其中包含一個選擇框。問題在於ng-options包含一個i18next文本列表,並且該視圖似乎在控制器之前已經「準備就緒」。如果這是有道理:) 我會更新我原來的帖子。 – 2014-10-28 13:43:15
所以在路由中,添加一個解決方案:{i18next:function(YourModule){return YourModulePromise}} – 2014-10-28 16:00:06