javascript Angular-translate 未知的提供者

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22330796/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 22:56:29  来源:igfitidea点击:

Angular-translate Unknown provider

javascriptangularjs

提问by Markuz Shultz

Im using angular translate in my app, this is my app.js

我在我的应用程序中使用角度翻译,这是我的 app.js

var formApp = angular.module('formApp', [
  'ngRoute',
  'ui.bootstrap',
  'pascalprecht.translate'
]).config(function($translateProvider) {
    $translateProvider.translations('en', {
       HEADLINE: 'Hello there, This is my awesome app!',
       INTRO_TEXT: 'And it has i18n support!'
    });
});

Im fallowing tutorial I fond on the internet.

我在互联网上喜欢的休闲教程。

This is how I loaded libraries in my html file

这就是我在 html 文件中加载库的方式

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

And this is how i print values

这就是我打印值的方式

<h2>{{ 'HEADLINE' | translate }}</h2>
<p>{{ 'INTRO_TEXT' | translate }}</p>

But when i open my app im getting this msg in console :

但是当我打开我的应用程序时,我会在控制台中收到此消息:

Error: [$injector:unpr] Unknown provider: translateFilterProvider <- translateFilter

Does anyone knows how to fix this? Im absolute beginner in angular, sorry if error is stupid :)

有谁知道如何解决这个问题?我绝对是 Angular 的初学者,如果错误很愚蠢,请见谅:)

回答by Wawy

In your controller, you need to add $translate as a dependency.

在您的控制器中,您需要添加 $translate 作为依赖项。

formApp.controller('myCtrl', function ($scope, $translate) {
 //code
});

So for example in the html, you must have a ng-controller at some point before those elements, thats the controller that needs the dependency injected as a parameter.

因此,例如在 html 中,您必须在这些元素之前的某个点有一个 ng-controller,即需要将依赖项作为参数注入的控制器。

<div ng-controller="myCtrl">
  ...
  <h2 translate>HEADLINE</h2>
  <p translate>INTRO_TEXT</p>
  ...
</div>

Also in the config block you need to set the prefered language:

同样在配置块中,您需要设置首选语言:

$translateProvider.preferredLanguage('en');