Coloniseur Coloniseur - 5 months ago 40
AngularJS Question

Use a service in Angular

I got some problem when I'm trying to use an Angular service in the controlleur of my application.

When I'm trying to use function of my service in my controlleur, my console throw me an error :/



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

app.config(['$routeProvider',
function ($routeProvider) {
$routeProvider
.when('/login', {
controlleur: 'login',
templateUrl: 'modules/login/login.html'
})

.otherwise({
redirectTo: '/login'
});
}]);

app.service('coreServices', [function () {
this.helloConsole = function () {
console.log("console services");
};
}]);

app.controller('loginController', ['$scope', '$http', '$rootScope', '$location', 'coreServices', LoginController]);

function LoginController($scope, $http, $rootScope, coreServices) {
var vm = this;

vm.helloConsole = coreServices.helloConsole;
vm.helloConsole();
}

angular.js:13708 TypeError: vm.helloConsole is not a function
at new LoginController





I link you this fiddle to show you how I do: https://jsfiddle.net/h8yaxLap/2/

The error throwed is:
angular.js:13708 TypeError: vm.helloConsole is not a function
at new LoginController

Answer

Well in your example angular will map $location to coreService in the injected parameters in the function. So I would go for

app.controller('loginController', ['$scope', '$http', '$rootScope', '$location', 'coreServices', LoginController]);
function LoginController($scope, $http, $rootScope, $location, coreServices)