2016-12-24 7 views
0

Ich habe ein Problem mit einer ionischen App. Ich erstelle eine service.js Datei, wo ich dieselbe Fabrik erstellen möchte.Ionic service.js funktionieren nicht

In meinem app.js Ich habe diese Zeile:

angular.module('app', ['ionic', 'app.controllers', 'app.routes', 'app.directives','app.services']) 

wo ich Controller, Routen laden, Richtlinien und Dienstleistungen.

Das ist mein services.js

angular.module('app.services', []) 

.factory('MyService', function() { 
    return { 
    sayHello: function() { 
     return "HELLO"; 
    } 
    } 
}); 

Wo neue Fabrik MyService mit einer Funktion sayHello genannt erstellen.

In meinem controller.js Ich habe diesen Code

angular.module('app.controllers', ['app.services'])  

.controller('iMontiCtrl', ['$scope', '$stateParams', 
function ($scope, $stateParams, MyService) { 

    $messaggio = "Test"; 
    alert($messaggio); 
    $scope.messaggio1 = MyService.sayHello(); 
    alert($messaggio+"2"); 
}]) 

Diese Show Code nur alert($messaggio) aber nicht alert($messaggio+"2")

Wenn ich Myservice.sayHello() Kommentar ist es gut funktionieren.

ich überprüfen index.html schließen auch ein:

<script src="js/app.js"></script> 


<script src="js/controllers.js"></script> 
<script src="js/routes.js"></script> 
<script src="js/directives.js"></script> 
<script src="js/services.js"></script> 

Aber ich sehe Problem nicht.

Antwort

0

Sie haben MyService in DI-Array zu injizieren, bevor es in Controller Factory Funktion

.controller('iMontiCtrl', ['$scope', '$stateParams', 'MyService', //<-- inject service here 
function ($scope, $stateParams, MyService) { 
+0

Dank verwenden. Es funktioniert gut! –

Verwandte Themen