AngularJS:浏览器忽略我的控制器定义(依赖注入)

时间:2014-04-17 15:34:28

标签: javascript angularjs angular-translate

我在项目中使用angular-translate但是我无法定义依赖注入$translate的控制器。代码不在浏览器中执行。我已经检查了JSHint ......

index.html

<html ng-app='ngApp'>
  <body>

    <div ng-controller="orderFormCtr">
        <ul>
            <li>{{'TITLE' | translate}}</li>
            <li translate="TITLE"></li>
        </ul>
    </div>

    <script src="bower_components/angular/angular.js"></script>
    <script src="bower_components/angular-translate/angular-translate.js"></script>
    <script src="app.js"></script>

  </body>
</html>

app.js

angular.module('ngApp', ['pascalprecht.translate']);

// this code works
angular.module('ngApp').config(['$translateProvider', function ($translateProvider) {
        $translateProvider.translations('en', {
            TITLE: 'Hello'
        });
        $translateProvider.translations('de', {
            TITLE: 'Hallo'
        });
}]);

// the browser ignores this code
angular.module('ngApp').controller('orderFormCtr', ['$scope', '$translate', function ($scope, $translate) {

    alert("Controller Code executed");

}]);

1 个答案:

答案 0 :(得分:1)

var app = angular.module('ngApp', ['pascalprecht.translate']);

// this code works
app.config(['$translateProvider', function ($translateProvider) {
        $translateProvider.translations('en', {
            TITLE: 'Hello'
        });
        $translateProvider.translations('de', {
            TITLE: 'Hallo'
        });
        $translateProvider.preferredLanguage('en');
        //or translateProvider.determinePreferredLanguage()
}]);

// the browser ignores this code
app.controller('orderFormCtr', ['$scope', '$translate', function ($scope, $translate) {

    alert("Controller Code executed");

}]);

http://jsbin.com/miqazola/1/