RuntimeException RuntimeException - 1 year ago 64
AngularJS Question

$inject in AngularJs

I have problem with inject a services to my controller. Below is the code and the result of this actions.

user.service.js:

class UsersService{
constructor(){}
usersServiceFn () {
return {
name: 'TEST'
};
};
}
export default UsersService;


users.jsp:

import UsersService from './users.service';

export default app => {
app.factory('users', UsersService);

if (ENVIRONMENT === 'test') {
require('./users.test.js');
}
}


service.js:

import usersService from './users/users';

export default app => {
INCLUDE_ALL_MODULES([usersService], app);
}


index.js:

import angular from 'angular';
import angularUIRouter from 'angular-ui-router';
import appComponents from './components/components.js';
import commonComponents from './common/components.js';
import appServices from './services/services.js';
import appConfiguration from './app.config';

// Single Style Entry Point
import './index.scss';

if (ENVIRONMENT === 'test') {
console.log('ENV:', ENVIRONMENT);
require('angular-mocks/angular-mocks');
}

const app = angular.module('app', ['ui.router']);
appComponents(app);
commonComponents(app);
appServices(app);


home.controller.js:

class HomeController {

constructor($scope, $http, usersService) {
this.$scope = $scope;
this.$http = $http;
this.usersService = usersService;
this.$scope.persons = null;
this.name = 'home';
this.$scope.surname ='ZZZZZZ';
this.$scope.randomNumb = Math.random();
this.initRecords();
this.range();
this.watchText();
}

/..../


}
HomeController.$inject = ['$scope', '$http', 'usersService'];
export default HomeController;


And the result:

enter image description here

Im trying but I don't know where is the problem.

**structure: **
enter image description here

Answer Source

Change this

app.factory('users', UsersService);

To

app.factory('usersService', UsersService);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download