2017-03-15 4 views
1

Ich erhalte Fehler in controller.jsAngular JS - ist keine Funktion

controller.js

angular.module('myApp', ['ngRoute', 'ui.router']).controller('authCtl', ["$scope", "$rootScope", "loginService", function($scope, $rootScope, loginService) { 
    $rootScope.bodyClass = "focusedform"; 
    $scope.submitForm = function() { 
    loginService.userAuth($scope.username, $scope.password); 
    } 
}]); 

factory.js

angular.module('myApp').factory('loginService', ["$http", "$q", function($http, $q) { 
    userFactory = {}; 
    userAuth = function(username, password) { 
    console.log(username + '' + password); 
    }; 
    return true; 
}]); 

enter image description here

+1

Sie sind wahr von Login-Dienst zurück. Gibt ein Objekt oder etwas mit der Funktion userAuth zurück. – Phix

Antwort

5

Sie didn‘ t kehren Sie Ihr Objekt in der Fabrik zurück.

Sie müssen Ihre Methodenvariable an ein Objekt anhängen und von der Fabrik zurückgeben, um auf den Controller zuzugreifen.

Versuchen Sie, wie dieses

angular.module('warApp').factory('loginService', ["$http", "$q", function($http, $q) { 
    var userFactory = {}; 
    userFactory.userAuth = function(username, password) { 
    console.log(username + '' + password); 
    }; 
    return userFactory; 
}]); 

oder versuchen, wie dies für eine bessere Ansicht

angular.module('warApp').factory('loginService', ["$http", "$q", function($http, $q) { 
    var userFactory = { 
    userAuth: userAuth 
    } 
    return userFactory; 
    function userAuth(username, password) { 
    console.log(username + '' + password); 
    } 
}]); 
+0

Dies funktioniert. Vielen Dank. –

Verwandte Themen