javascript - 如何在 AngularJS 上放置服务?

标签 javascript angularjs dependency-injection angularjs-factory

我正在尝试在 Angular js 上编写一个模块来放置我的服务(文件:utils/productos.js),然后将其加载为我的模块 ventas(ventas/ventas.js)的依赖项,但是

我收到下一个错误:

Error: [$injector:nomod] Module 'utils.producto' is not available! You either misspelled the module name or forgot to load it. If registering a module ensure that you specify the dependencies as the second argument.
http://errors.angularjs.org/1.3.10/$injector/nomod?p0=utils.producto angular.js:63:11
Error: [$injector:modulerr] Failed to instantiate module webui due to:
[$injector:modulerr] Failed to instantiate module utils.producto due to:
[$injector:nomod] Module 'utils.producto' is not available! You either misspelled the module name or forgot to load it. If registering a module ensure that you specify the dependencies as the second argument.
http://errors.angularjs.org/1.3.10/$injector/nomod?p0=utils.producto
minErr/<@http://localhost/cat-dog/web/app/js/angular.js:63:12
module/<@http://localhost/cat-dog/web/app/js/angular.js:1764:1
ensure@http://localhost/cat-dog/web/app/js/angular.js:1688:38
module@http://localhost/cat-dog/web/app/js/angular.js:1762:1
loadModules/<@http://localhost/cat-dog/web/app/js/angular.js:4094:22
forEach@http://localhost/cat-dog/web/app/js/angular.js:323:11
loadModules@http://localhost/cat-dog/web/app/js/angular.js:4078:5
loadModules/<@http://localhost/cat-dog/web/app/js/angular.js:4095:40
forEach@http://localhost/cat-dog/web/app/js/angular.js:323:11
lo

app.js

var app = angular.module('webui', ['ngRoute', 'utils.producto', 'ventas']);

main.html

<script type="text/javascript" src="ventas/ventas.js"></script>
<script type="text/javascript" src="js/app.js"></script>
<script type="text/javascript" src="utils/productos.js"></script>

ventas/ventas.js

angular.module('ventas', ['ngRoute', 'utils.producto'])

        .config(['$routeProvider', function ($routeProvider) {
                $routeProvider.when('/ventas', {
                    templateUrl: 'ventas/ventas.html',
                    controller: 'VentasCtrl'
                });
            }])

utils/producto.js

    angular.module('utils.producto').factory("buscadorProductos", buscadorProductos).factory("autocompProductos", autocompProductos);

    function buscadorProductos($http) {
..

最佳答案

首先将服务定义为模块,例如angular.module('utils.producto',[])

代码

 angular.module('utils.producto',[]).factory("buscadorProductos", buscadorProductos).factory("autocompProductos", autocompProductos);

    function buscadorProductos($http) {

关于javascript - 如何在 AngularJS 上放置服务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29357125/

相关文章:

javascript - 在 Angular 上迭代服务返回的 JSON

java - Angular Material - 如何检查 selenium java 中的复选框是否被选中?

c# - .NET Core DI,为包注册默认实现

typescript - 'Class' 在它自己的类型注释中被直接或间接引用

javascript - 当我单击关闭时,Bootstrap 3 模态不会删除

javascript - 自定义属性 - 是还是不是?

javascript - 优化数组的结果

javascript - 如何动态加载javascript文件?

angularjs - Angular UI-Router 在重新加载时重置查询字符串参数

ios - Objective c 初始值设定项的歧义