2016-01-27 3 views
-1

я получаю следующее сообщение об ошибке:не в состоянии позвонить в службу в angularjs

Error: [$injector:unpr] Unknown provider: myServiceProvider <- myService 

при вызове службы в angularjs, пожалуйста, помогите мне решить эту проблему

var app = angular.module('app', []) 

app.controller('myController', ['$scope', 'stringService', function ($scope, stringService) { 
    $scope.output = stringService.processString(input); 
}]); 

var app = angular.module('app', []); 
app.factory('stringService', function(){ 
    return{ 
     processString: function(input){ 
      if(!input){ 
       return input; 
      } 

      var output = ""; 
      for(var i = 0; i < input.length; i++){ 
       if(i > 0 && input[i] == input[i].toUpperCase()){ 
        output = output + " "; 
       } 
       output = output + input[i]; 
      } 
      return output; 
     } 
    } 
}); 
+0

вставить код, или предоставить рабочий plnkr –

+0

var app = angular.module ('app', []), отсутствует ";" – Miha2255

ответ

0

В:

['$scope', 'myService', function ($scope, stringService) 

Вы вводите myService, которого нет. Таким образом, изменение:

['$scope', 'stringService', function ($scope, stringService) 
0

Вы определяете свой завод в stringService, но вы пытаетесь внедрить его как myService, если вы делаете это следующим образом она должна работать:

app.controller('myController', ['$scope', 'stringService', function ($scope, stringService) { 
    $scope.output = stringService.processString(input); 
}]); 
0

Измените свой второй модульная линия к этому:

var app = angular.module('app'); 

Если вы передаете 2 параметра, он определяет новый модуль. Если вы передаете один параметр, то он выполняет поиск по модулю.

Вы должны определять модуль один раз.

0

код определено два приложения с именем приложения, MYSERVICE не существует, то он должен измениться stringService, я думаю, что ваш код должен быть таким:

var app = angular.module('app', []); 
app.factory('stringService', function(){ 
return{ 
processString: function(input){ 
if(!input){ 
return input; 
} 
var output = ""; 
for(var i = 0; i < input.length; i++){ 
if(i > 0 && input[i] == input[i].toUpperCase()){ 
output = output + " "; 
} 
output = output + input[i]; 
} 
return output; 
} 
} 
}); 


app.controller('myController', 
['$scope', 'stringService', function ($scope, stringService) { 
$scope.output = stringService.processString(input); 
}]); 
Смежные вопросы