angular js 未知提供者 [英] angular js unknown provider

查看:31
本文介绍了angular js 未知提供者的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试自定义"mongolab 示例以适合我自己的 REST API.现在我遇到了这个错误,我不确定我做错了什么:

I'm trying to "customize" the mongolab example to fit my own REST API. Now I'm running into this error and I am not sure what I am doing wrong:

Error: Unknown provider: ProductProvider <- Product
    at Error (unknown source)
    at http://localhost:3000/js/vendor/angular.min.js:28:395
    at Object.c [as get] (http://localhost:3000/js/vendor/angular.min.js:26:180)
    at http://localhost:3000/js/vendor/angular.min.js:28:476
    at c (http://localhost:3000/js/vendor/angular.min.js:26:180)
    at d (http://localhost:3000/js/vendor/angular.min.js:26:314)

这是我的控制器:

function ProductListCtrl($scope, Product) {
  $scope.products = Product.query();
}

这是模块:

angular.module('productServices', ['ngResource']).
    factory('Product', ['$resource', function($resource){
      var Product = $resource('/api/products/:id', {  }, {
        update: { method: 'PUT' }
      });

      return Product;
    }]);

推荐答案

你的代码看起来不错,实际上它在复制和复制时有效(除了调用本身)粘贴到示例 jsFiddle 中:http://jsfiddle.net/VGaWD/

Your code looks good, in fact it works (apart from the calls themselves) when copied & pasted into a sample jsFiddle: http://jsfiddle.net/VGaWD/

如果没有看到更完整的示例,很难说发生了什么,但我希望上面的 jsFiddle 会有所帮助.我怀疑您没有使用productServices"模块初始化您的应用.它会给出同样的错误,我们可以在另一个 jsFiddle 中看到:http://jsfiddle.net/a69nX/1/

Hard to say what is going on without seeing a more complete example but I hope that the above jsFiddle will be helpful. What I'm suspecting is that you are not initializing your app with the 'productServices' module. It would give the same error, we can see this in another jsFiddle: http://jsfiddle.net/a69nX/1/

如果您打算使用 AngularJS 和 MongoLab 我建议为 $resource 和 MongoLab 使用现有的适配器:https://github.com/pkozlowski-opensource/angularjs-mongolab它减轻了使用 MongoLab 的大部分痛苦,您可以在这里看到它的实际效果:http://jsfiddle.net/pkozlowski_opensource/DP4Rh/免责声明!我正在维护这个适配器(基于 AngularJS 示例编写),所以我显然有偏见.

If you are planning to work with AngularJS and MongoLab I would suggest using an existing adapter for the $resource and MongoLab: https://github.com/pkozlowski-opensource/angularjs-mongolab It eases much of the pain working with MongoLab, you can see it in action here: http://jsfiddle.net/pkozlowski_opensource/DP4Rh/ Disclaimer! I'm maintaining this adapter (written based on AngularJS examples) so I'm obviously biased here.

这篇关于angular js 未知提供者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆