Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在AngularJS服务中加载模块_Javascript_Angularjs - Fatal编程技术网

Javascript 在AngularJS服务中加载模块

Javascript 在AngularJS服务中加载模块,javascript,angularjs,Javascript,Angularjs,我在学AngularJS。目前,我正在尝试在我的服务中加载第三方模块。具体地说,我试图加载角力矩。我的服务定义如下: myApp.factory('myService', ['angularMoment', function(angularMoment) { return { getLocale: function() { return angularMoment.locale(); } } }]); var myApp = angular.module("

我在学AngularJS。目前,我正在尝试在我的服务中加载第三方模块。具体地说,我试图加载
角力矩
。我的服务定义如下:

myApp.factory('myService', ['angularMoment', function(angularMoment) {
  return {
    getLocale: function() {
      return angularMoment.locale();
    }
  }
}]);
var myApp = angular.module("myApp", ["angularMoment"]);
如果我将return
angularmonent.locale()
替换为
return'someLocale'我的代码运行。然而,只要我引用
角度矩
,我就会得到错误。我知道这与我没有正确加载模块有关。然而,我不知道我做错了什么。我只是在运行单元测试时看到此错误:

Error: [$injector:unpr] http://errors.angularjs.org/1.2.22/$injector/unpr?p0=angularMomentProvider%20%3C-%20angularMoment%20%3C-myService (line 36) (1)

我做错了什么?

在声明模块时,需要引用
angularMoment
模块。否则它将无法加载。这就是导致
$injector:unpr
错误的原因。所以你需要这样的东西:

myApp.factory('myService', ['angularMoment', function(angularMoment) {
  return {
    getLocale: function() {
      return angularMoment.locale();
    }
  }
}]);
var myApp = angular.module("myApp", ["angularMoment"]);

这听起来很基本,但您在html代码中引用了.js文件,对吗?

在服务/因子注入器中尝试“矩”而不是Angular矩。这将为您提供来自MomentJS的对象

myApp.factory('myService', ['moment', function(moment) {
  return {
    getLocale: function() {
      return moment.locale();
    }
  }
}]);
这对我很有用:

var-app=angular.module('myApp',['angularmonent']);
app.controller('myCtrl',函数($scope,$log,moment){
var now=力矩();
$log.info(now.format(“dddd,MMMM-Do-YYYY,h:mm:ss-a”);

});
您是否主要按照标签的顺序使用了所有内容?需要先加载angularMoment js文件,然后再加载应用程序js文件。也就是说,在将角力矩注入工厂之前,需要对其进行定义。确保文件加载顺序正确。没有名为
angularmonent
的服务,也没有
.locale()
方法。可能它与@bhantol提到的图书馆不同?我实际上没有网页。我正在建立一些AngularJS服务。我在我的Jasmne测试中遇到了这个错误。有相同的问题,对此有什么结论吗?我在我的Jasmine任务中引用了.js文件。我正在通过Grunt运行代码。