2015-03-19 20 views
0

我所指的translateProvider像這樣:在angularJS服務如何我可以用MYPROVIDER

var myAppModule = angular.module('myapp', ['ngRoute', 'ngMaterial', 'ngResource', 'pascalprecht.translate']);

開始JS,我想用在我的commonService.js, 像這樣:

myAppModule.factory('commonService', ['$resource', '$translate', function ($resource, $translate) { 
 
     var auth = $resource('http://localhost:8012/user/auth.aspx', {}, {}); 
 
     return { 
 
      init: function() { 
 
       var language = $resource("/Resources/Chinese.json"); 
 
       language.get(function (data) { 
 
        console.data(data); 
 
       }); 
 
       $translate.translations('en', { 
 
        HEADLINE: 'Hello there, This is my awesome app!', 
 
        INTRO_TEXT: 'And it has i18n support!' 
 
       }) 
 
      .translations('de', { 
 
       HEADLINE: 'Hey, das ist meine großartige App!', 
 
       INTRO_TEXT: 'Und sie untersützt mehrere Sprachen!' 
 
      }); 
 
       $translate.preferredLanguage('de'); 
 
      }, 
 
      submit: function (user) { 
 
       user.uk = "ad82544450d6fd3296878cbcd7a95d6b"; 
 
       return auth.get(user); 
 
      } 
 
     } 
 
    }]);

但它警告我: 類型錯誤:未定義是不是Object.init功能 (commonService.js:14): 在Object.invoke(angular.js在新的(8 loginController.js): 4182 ) 在$ get.extend.instance(angular.js:8441) 在angular.js:7693 在的forEach(angular.js:331) 在nodeLinkFn(angular.js:7692) 在compositeLinkFn(angular.js :7075) 在publicLinkFn(angular.js:6954) 在angular.js:1451個 angular.js:9814 GET http://localhost:41251/Resources/Chinese.json 404(未找到)

回答

0

順便說一句,你得到的錯誤,只是說中文.json 的路徑是錯誤的,嘗試打開它在不同的選項卡,它會給你相同的404。所以提供正確的路徑到JSON文件。 我沒有使用角度翻譯,但從那裏的例子,它似乎你以錯誤的方式使用它。 基本上,一個提供者只能在配置階段才能使用。 從AngularJs網站

During application bootstrap, before Angular goes off creating all services, it configures and instantiates all providers. We call this the configuration phase of the application life-cycle. During this phase, services aren't accessible because they haven't been created yet.

Once the configuration phase is over, interaction with providers is disallowed and the process of creating services starts. We call this part of the application life-cycle the run phase.

您需要設置首選語言和翻譯過程中的配置階段, ,你可以使用$翻譯服務 使用方法切換語言在應用程序中。下面的代碼是從鏈路http://angular-translate.github.io/ 此外,他們給了一步這裏http://angular-translate.github.io/docs/#/guide/07_multi-language 用於異步加載例子一步文檔都可以在這裏http://angular-translate.github.io/docs/#/guide/12_asynchronous-loading

var myAppModule = angular.module('myapp', ['ngRoute', 'ngMaterial',  'ngResource', 'pascalprecht.translate']); 

myAppModule.config(function ($translateProvider) { 
    $translateProvider.translations('en', { 
    HEADLINE: 'Hello there, This is my awesome app!', 
    INTRO_TEXT: 'And it has i18n support!' 
    }); 
    $translateProvider.translations('de', { 
    HEADLINE: 'Hey, das ist meine großartige App!', 
    INTRO_TEXT: 'Und sie untersützt mehrere Sprachen!' 
    }); 
    $translateProvider.preferredLanguage('en'); 
}); 

myAppModule.controller('Ctrl', function ($scope, $translate) { 
    //key is 'en','de' i.e. the language you 
    //you want to use 
    $scope.changeLanguage = function (key) { 
    $translate.use(key); 
    }; 
}); 

+0

發現太謝謝你了。 – 2015-03-19 06:12:41

相關問題