使用angular js中的外部模块服务解析状态

Resolve state using external module service in angular js

本文关键字:服务 状态 模块 外部 angular js 使用      更新时间:2023-09-26

我在模块1:中创建了一个工厂

angular.module('module1',[]);
angular.module('module1').factory('factory1', function($resource, $q){
  return 'prmose response';//This will include promise
});

我想调用这个工厂来解决在模块2:中创建的状态

angular.module('module2',['module1']);
angular.module('module2').config(['$stateProvider', function($stateProvider){
  $stateProvider.state('stateName',{
    url: '/url',
    templateUrl: '/url/path.html',
    controller: 'controllerName',
    reolve:{
      data: function(factory1){
        return factory1;
      }
    }
  })
}])

当我使用来自module2的服务时,它可以工作,但当使用来自另一个模块的任何服务时它不工作。

请hrlp我解决这个问题。

提前感谢

当您有两个模块时,您可以在它们的分离中依赖模块A和模块B

这公开了每个的所有服务、指令、工厂等。

你只需做

angular.module('A', ['b'])

这意味着您正在创建一个名为A的模块。后面的数组是模块A所需的依赖项列表。只要所有这些依赖项都包含在页面上(我相信在您的模块A之前),那么它就会全部工作,并允许您从每个依赖项中注入内容。

//pseudo example
angular.module('B', [])
.service('myService', function () {
    this.hey = function () {
        console.log('You guys!');
    }
});
angular.module('A', ['B'])
.controller('test', function ($scope, myService) {
    myService.hey()' //You guys!
});

以下列出了有关该主题的一些有用信息:https://docs.angularjs.org/guide/module