Angular ui-route解决注入问题

时间:2016-11-17 14:26:54

标签: angularjs

我是Angular JS的新手,我在向控制器注入解析承诺时遇到了问题。

我有下一个代码:

var app = angular.module('app', ['ui.router', 'ngAnimate', 'ngSanitize', 'ui.bootstrap'])
        .config(function ($stateProvider, $urlMatcherFactoryProvider, $urlRouterProvider) {
            $urlMatcherFactoryProvider.caseInsensitive(true);
            $urlRouterProvider.otherwise('/refuel');
            $stateProvider.state('refuels', {
                url: '/refuel',
                controller: 'refuelController',
                controllerAs: 'refuelCtrl',
                resolve: {
                    $refuelsPumpsResolve: function ($http) {
                        return $http({
                            url: "http://localhost:60000/Refuels/GetUserPumps",
                            method: "GET"
                        })
                    }
                }
            })
        })
        .controller('refuelController', function ($refuelsPumpsResolve) {
            var $this = this;
            this.isOpen = true;
            this.isOpen = function () {
                $this.isOpen = !$this.isOpen
            }
            this.pumpsData = $refuelsPumpsResolve;

        });

然而,角度会在控制器中抛出$ refuelsPumpsResolve的“未知提供者”异常。 我没有看到任何问题,更多的代码来自github上的ui-route教程。 谢谢你的帮助

1 个答案:

答案 0 :(得分:0)

试试这个,像通常那样声明注射,控制器:

resolve: {
  $refuelsPumpsResolve: ['$http', function ($http) {
    return $http({
      url: "http://localhost:60000/Refuels/GetUserPumps",
      method: "GET"
    })
  }]
}