angularJS,注入服务后,它仍然报告unknow provider

时间:2016-11-20 23:43:08

标签: angularjs

我已将新服务注入我的控制器,但它总是说未知提供商

我的代码: 出口服务:

 class AlertService {
    constructor($rootScope,$injector) {
        this.rootScope = $rootScope;
        this.injector = $injector;
    }

    showAlert(alertTitle='Alert',alertBody='') {
        this.injector.get('$uibModal').open({
        template: '<header><div class="alert-header"><h1>' + alertTitle + '</h1></div><div class="alert-close" ng-click="ok()"><i class="icon icon--close" aria-hidden="true"></i></div></header>'
         + '<section class="content-section content-section-alert"><p>' + alertBody + '</p></section>'
         + '<footer><button class="btn btn-primary" ng-click="ok()">ok</button>',
        controller: function($scope, $uibModalInstance,$rootScope) {
            $rootScope.ok = function() {
                $uibModalInstance.dismiss('cancel');
            };
        },
        windowClass: 'alert-modal'
      });
    }
}

AlertService.$inject = ['$rootScope','$injector'];

export default angular.module('services.alertService', [])
    .service('alertService', AlertService)
    .name;

将服务注入控制器:

    export default class StartController {
        constructor($scope, alertService, $location){
            this.alertService = alertService
            this.scope = $scope
            this.location = $location

            this.watchUrl()
        }

        watchUrl() {
           let url = this.location.url()
           if(url.indexOf('start') == -1)
              return

           this.alertService.showAlert('alert','some error')
        }
    }
StartController.$inject = ['$scope', 'alertService', '$location'];

它总会报告未知的提供者:

Unknown provider: tProvider <- t

如果我将alertService更改为alert(),那么它可以正常工作

原因是什么?感谢

1 个答案:

答案 0 :(得分:0)

您尚未为模态控制器提供依赖注入注释

var createIndexResponse = client.CreateIndex("index-name", c => c
    .Settings(s => s
        .NumberOfShards(1)
        .NumberOfReplicas(0)
    )
    .Mappings(m => m
        .Map<Conference>(d => d
            .AutoMap()
        )
    )
);

将其提取到函数或类中,以便设置controller: function($scope, $uibModalInstance, $rootScope) { ... } 属性或使用数组注释,例如

$inject