2014-10-28 92 views
3

我想在我的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")}, 
    ]; 
+1

嘿,是不是你的控制器鏈接到一個視圖?如果是這樣,您可以使用路徑上的解決方案來等待模塊加載。但通常角將等待,直到所有模塊加載之前運行...該模塊中是否有一個Ajax請求? – 2014-10-28 13:07:57

+0

它鏈接到一個視圖,其中包含一個選擇框。問題在於ng-options包含一個i18next文本列表,並且該視圖似乎在控制器之前已經「準備就緒」。如果這是有道理:) 我會更新我原來的帖子。 – 2014-10-28 13:43:15

+1

所以在路由中,添加一個解決方案:{i18next:function(YourModule){return YourModulePromise}} – 2014-10-28 16:00:06

回答

1

這解決了我的問題。

angular.module('myApp', ['ngRoute', 'jm.i18next']). 
     config(['$routeProvider', function ($routeProvider) { 
      $routeProvider. 
       when('/route1', { 
        title: 'Route 1', 
        templateUrl: '../views/route1.html', 
        controller: 'Route1Ctrl', 
        resolve: { 
         i18next: function ($i18next) { 
         return $i18next; 
         } 
        } 
       }); 
    }]); 
+1

我有同樣的問題,這解決了它。雖然海事組織這是一個醜陋的解決方案,因爲你必須在每條路線上放置這個解決方案功能 – karantan 2015-07-23 12:41:39

0

你也可以聽i18nextLanguageChange事件:

$scope.$on('i18nextLanguageChange', function() { 
    $scope.workouts = [ 
    {id: 1, name: 'Workout 1', type: $i18next("type1")}, 
    {id: 25, name: 'Workout 2', type: $i18next("type2")}, 
    ]; 
}); 

你必須解決你自己這樣其他競爭條件。