需要解决服务中断



尝试访问解析中的服务:

angular
  .module('app', ['ui.router', 'templates'])
  .config(function($stateProvider, $urlRouterProvider) {
    $stateProvider
      .state('home', {
        url: '/',
        templateUrl: 'app/views/home.html',
        controller: 'Home as home',
        resolve: {
          productIndex: function (ProductService) {
debugger;
            // return ProductService.getProductsIndex();
          }
        }
      });
    $urlRouterProvider.otherwise('/');
  });

当运行上面的代码时,调试器永远不会命中,控制台中也不会出现任何内容。当ProductService作为参数被删除时,调试器会命中,但很明显,无法调用该服务。如果可能的话,我也更愿意只从服务进行http调用。

我已经四处寻找了一段时间,只看到了类似注射的有效例子。即使回顾以前的(工作中的)项目,我也看不出与上面所做的有什么不同。我的猜测是其他地方可能出了问题。任何帮助都会很棒!

为了完整起见:

function ProductService($http) {
  this.getProductsIndex = function() {
    // debugger;
    // return $http.get('/products');
  };
}
angular
  .module('app')
  .controller('ProductService', ProductService);

现在您的服务正在注册为控制器。您应该将其注册为服务。例如

angular
  .module('app')
  .service('ProductService', ProductService);

var app=角度模块('*****')

app.service('ProductService', function() {
        return {
            getData: function($q, $http) {
                var defer = $q.defer();
                $http.get('****').success(function(data) {
                    defer.resolve(data);            
                });
                return defer.promise;
            }
        };
    });

abou解析

resolve取服务的字符串或返回要注入的值的函数

resolve: {
  productIndex: function (ProductService) {
   // your stuff goes here.
  }
}

这应该有效..:)

最新更新