Z
Z
zigen2016-02-18 10:12:56
Angular
zigen, 2016-02-18 10:12:56

How to properly use modules in AngularJS?

Good afternoon.
Decided to play around with AngularJS 1.4.9 as a front for the REST backend.
Studied the sample application phonecat. According to him, you need to use modules as follows:
app.js

var sharePhotoApp = angular.module('sharePhotoApp', [
  'userControllers',
  'userServices'
]);

controllers.js

var userControllers = angular.module('userControllers', []);

userControllers.controller('UserListCtrl', function ($scope, $http) {
   $http({ method: 'GET', url: '/api/users'}).then(function successCallback(response) { $scope.users = response.data;});
});

However, in this case I get an error:
Error: $injector:modulerr
Module Error

Works in the following version:
var userControllers = angular.module('sharePhotoApp', []);

However, when I look at Hints through Angular Batarangs I see the following:
8482fb95b64e4e6ebee44e3861d1359f.jpg
WTF? How to do it right? :)
############################## Tell me
what I'm doing wrong when I try to create a resource for the API
angular
    .module('sharePhotoApp')
    .factory('userService', userService);

    userService.$inject = ['$resource'];

function userService($resource) {
    return $resource('/api/users/:username', {}, {
          query: {method:'GET', params:{username:'@username'}, isArray:true}
          });
}

angular
    .module('sharePhotoApp')
    .controller('UserListCtrl', UserListCtrl);

    UserListCtrl.$inject = ['$scope', 'userService'];

function UserListCtrl($scope,  userService) {
    $scope.apiusers = userService.query();
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
rakro, 2016-02-18
@zigen

https://github.com/johnpapa/angular-styleguide#sty...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question