2016-11-20 3 views
0

ich den neuen Dienst in meinen Controller gespritzt habe, aber es sagt immer unbekannt AnbieterAngularJS, nach dem Dienst Injektion, meldet es noch unbekannter Anbieter

mein Code: Exportservice:

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; 

die injizieren Service in der Steuerung:

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']; 

es würde immer melden unbekannt Anbieter:

Unknown provider: tProvider <- t 

wenn ich die alertService zu alert() ändern, dann funktioniert es

, was der Grund sein? Dank

+0

Vermutlich liegt das Problem in 'AlertService'. Wie sieht es aus? – Phil

+1

In welchem ​​Modul befindet sich Ihr Controller? Ist dieses Modul von 'services.alertService' abhängig? – Phil

+0

@Phil, Ich habe den Alarm eingefügtService-Code –

Antwort

0

Sie haben nicht Dependency Injection Anmerkungen für die modale des Controllers vorgesehen

controller: function($scope, $uibModalInstance, $rootScope) { ... } 

Entweder es auf eine Funktion oder Klasse extrahieren, so dass Sie die $inject Eigenschaft oder verwenden Sie das Array Annotation festlegen können, zB

controller: ['$scope', '$uibModalInstance', '$rootScope', 
      function($scope, $uibModalInstance, $rootScope) { ... }] 
+0

speicherte meinen Tag! danke –

+0

@BenjaminLi du bist willkommen – Phil

Verwandte Themen