multiple configuration in AngularJS module

非 Y 不嫁゛ 提交于 2019-12-11 20:13:37

问题


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


回答1:


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'];



回答2:


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);


来源:https://stackoverflow.com/questions/33971500/multiple-configuration-in-angularjs-module

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!