multiple configuration in AngularJS module

2019-09-06 05:28发布

I am using ng-route and ng-translate at the same time, where both require configuration in the module. Apparentely my routing is working but my ng-translate is haveing problems of being in the same confing with routing configuration.

"use strict";
(function(){
var app = angular.module('ratingApp', ['ngRoute', 'ui.bootstrap', 'ngMessages', 'ngFileUpload', 'ngAnimate', 'pascalprecht.translate']);
app.config(function($routeProvider, $translateProvider){
    $routeProvider
        .when('/', {
            templateUrl: 'views/list.html',
            controller: 'candidatescontroller'
            })
        .when('/candidate/:candiateIndex', {
            templateUrl: 'views/candiate.html',
            controller: 'candidatecontroller'
            })
    // .when('', )
        .otherwise({ redirectTo: '/'});
    $translateProvider.translations('en', {
        'TOPIC':'Candidate Poll'
            });
    $translateProvider.translations('fr', {
        'TOPIC':'Poll de Candidate'
            });
    $translateProvider.preferredLanguage('en'); 

});
}());

Here is the error that I get:

Unknown provider: $translateProvider <- $translate <-andidatescontroller

BTW: here is the end of my index.html in the term of inclduing requirements

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
    <script src="js/vendor/bootstrap.min.js"></script>
    <script src="js/vendor/angular.min.js"></script>
    <script src="js/vendor/angular-translate.min.js"></script>  
    <script src="js/vendor/angular-messages.min.js"></script>
    <script src="js/vendor/angular-route.min.js"></script>
    <script src="js/vendor/ui-bootstrap-tpls.min.js"></script>
    <script src="js/vendor/ng-file-upload-shim.min.js"></script> <!-- for no html5 browsers support -->
    <script src="js/vendor/ng-file-upload.min.js"></script> 
    <script src="js/vendor/angular-animate.min.js"></script>  
    <script src="js/rateApp.js"></script>
    <script src="js/services/candidates.js"></script> 
    <script src="js/services/modal.js"></script>       
    <script src="js/candidatescontroller.js"></script>
    <script src="js/candidecontroller.js"></script>

Controller codes related to ng-tranlate:

use strict";
(function(){
var candidatescontroller = function($scope, $http, candidatesFactory, $timeout, modalService, $location, $anchorScroll, $translate){
    $scope.changeLanguage = function (key) {
            $translate.use(key);
    };
candidatescontroller.$inject = ['$scope', '$http', 'candidatesFactory', '$timeout', 'modalService', '$location', '$anchorScroll', ' $translate'];
angular.module('ratingApp').controller('candidatescontroller', candidatescontroller);
}());

I am following ng-newsletter article, and it worked for me when I used it in another app without routing,

Also, my routing was working before adding this ng-translate configurations to the module.

A live demo of my code can be found here

2条回答
别忘想泡老子
2楼-- · 2019-09-06 05:58

Inside your candidatecontroller.js file at the bottom, you have a space injected with '$translate', remove the space

candidatescontroller.$inject = ['$scope', '$http', 'candidatesFactory', '$timeout', 'modalService', '$location', '$anchorScroll', '$translate'];
angular.module('ratingApp').controller('candidatescontroller', candidatescontroller);
查看更多
爱情/是我丢掉的垃圾
3楼-- · 2019-09-06 06:01

you have a space in $translate

candidatescontroller.$inject = ['$scope', '$http', 'candidatesFactory', '$timeout', 'modalService', '$location', '$anchorScroll', ' $translate'];

remove it

candidatescontroller.$inject = ['$scope', '$http', 'candidatesFactory', '$timeout', 'modalService', '$location', '$anchorScroll', '$translate'];
查看更多
登录 后发表回答