2

我正在尝试在我的 ui-router 配置文件 routes.js 中使用 $translate.use() 所以我可以切换切换语言的状态,但我得到了

未捕获的错误:[$injector:modulerr] 无法实例化模块 frontApp 由于:错误:[$injector:unpr] 未知的提供者:$translate

这是我的文件。

'use strict';

(function() {
    angular.module('frontApp')
        .run(['$rootScope', '$state', '$stateParams', function($rootScope, $state, $stateParams) {
            $rootScope.$state = $state;
            $rootScope.$stateParams = $stateParams;
        }]
    )
    .config(['$urlRouterProvider', '$stateProvider', '$locationProvider', '$translate', function($urlRouterProvider, $stateProvider, $locationProvider, $translate){
        $urlRouterProvider
            .otherwise('/');
        $stateProvider
            .state('home', {
                url: '/',
                templateUrl: '/views/home.html',
                controller: 'HomeCtrl'
            })
            .state('home.rules', {
                url: '^/rules',
                templateUrl: '/views/rules.html'
            })
            .state('home.terms', {
                url: '^/terms',
                templateUrl: '/views/terms.html'
            })
            .state('home.faq', {
                url: '^/faq',
                templateUrl: '/views/faq.html'
            })
            .state('language', {
                controller: function() {
                    console.log('ok');
                    $translate.use(($translate.use() === 'fr') ? 'en' : 'fr' );
                }
            });
        $locationProvider.html5Mode(true);
    }]);
}());

我还有一个包含我的翻译的 translation.js 文件,当不尝试在上述 routes.js 文件中注入和使用 $translate.use() 时,我使用 TranslateController 使用它没有问题:

'use strict';

(function() {
    angular.module('frontApp')
        .config(['$translateProvider', function($translateProvider) {
            $translateProvider
                .translations('fr', {
                    language: 'English',
                    otherLanguage: 'en',
                    cssClass: 'french',
                    linkHome: 'Accueil',
                    linkRules: 'Règlement',
                    linkTerms: 'Termes et conditions',
                    linkFAQ: 'FAQ',
                })
                .translations('en', {
                    language: 'Français',
                    otherLanguage: 'fr',
                    cssClass: 'english',
                    linkHome: 'Home',
                    linkRules: 'Rules',
                    linkTerms: 'Terms and conditions',
                    linkFAQ: 'FAQ',
                });
            $translateProvider.preferredLanguage('fr');
        }])
        .controller('TranslateController', ['$translate', '$scope', '$location', function ($translate, $scope, $location) {
            // The URLs need to be updated
            var host = $location.host();
            if ( host === 'localhost' || host === '0.0.0.0') {
                $translate.use('fr');
            }
            else if (host === 'localhost-en') {
                $translate.use('en');
            }
            $scope.switchLanguage = function() {
                $translate.use(($translate.use() === 'fr') ? 'en' : 'fr' );
            };
        }]);
}());

这是 app.js:

'use strict';

angular
  .module('frontApp', [
    'ui.router',
    'ngAnimate',
    'ngCookies',
    'ngResource',
    'ngSanitize',
    'ngTouch',
    'pascalprecht.translate'
  ]);

我错过了什么?非常感谢您的时间和帮助。

4

1 回答 1

3

这里的解决方案是将'$translate'的DI定义移动到它所属的位置,即进入controller定义:

 .state('language', {
 // instead of this
 // controller: function() {

 // use this
 controller: function($translate) {

      console.log('ok');
      $translate.use(($translate.use() === 'fr') ? 'en' : 'fr' );
    }
 });

甚至更好controller: ['$translate', function($translate) {... }]

为什么当前的解决方案不起作用?好吧,让我们看一下上面代码的摘录:

// I. CONFIG phase
.config(['$urlRouterProvider', '$stateProvider', '$locationProvider', '$translate'
       , function($urlRouterProvider, $stateProvider, $locationProvider, $translate){
    ...
    $stateProvider
        ...
        .state('language', {
            // II. CONTROLLER - RUN phase
            controller: function() {

如我们所见,我们在配置阶段要求$translate上述内容。所以我们收到要配置的提供程序。

但是控制器定义预计将提供服务 - 已配置。它必须有自己的 DI ... 因为它将在 RUN 阶段执行...

另请查看此问答以获取有关配置与运行阶段提供程序的更多详细信息 - 出现错误:未知提供程序:$urlMatcherFactory

于 2015-01-10T18:35:15.403 回答