AngularJS - Why are dependencies with modules needed?

Can you say why it is recommended to add dependency modules to module creation?

I tried to run the code without adding TodoApp.controllers and TodoApp.services modules . Despite the fact that I deleted them, everything went fine. (I call the service through these two files)

app.js

// Injects all the needed modules
var TodoApp = angular.module("TodoApp", [
        "ngRoute",
        "ngResource",
        "TodoApp.controllers",
        "TodoApp.services"
]).
    config(function ($routeProvider) {
        $routeProvider.
            when('/', { controller: "listCtrl", templateUrl: 'list.html' }).
            otherwise({ redirectTo: '/' });
    });

Update

Thanks for your reply!:)

Unfortunately, these files do not have a common variable:

That was the reason I did not understand the problem. For me, this code is stored nowhere. Maybe because of the dot notation in the name?

controller.js

angular.module('TodoApp.controllers', []).
    controller('listCtrl', function ($scope, $location, todoApiService) {
        $scope.todos = todoApiService.getMyTodos().query();
});  

services.js

angular.module('TodoApp.services', []).
    factory('todoApiService', function ($resource) {
        var todoApi = {};

        todoApi.getMyTodos = function () {
            return $resource('/api/todo/:id', { id: '@id' }, { update: { method: 'PUT' } });
        };

    return todoApi;
});
+4
source share
1

, module.

:

TodoApp.controllers(/* ... */)
TodoApp.services(/* ... */)

- TodoApp ( angular.module('TodoApp').

, :

angular.module('TodoApp.controllers', []).
    controller('listCtrl', /* etc etc */ );

( ) angular TodoApp, .

, , // ..

var HelperModule = angular.module('HelperModule', /*...*/);
HelperModule.directive('headerLinks', /*...*/);
HelperModule.factory('BaseFactory', /*...*/);
// etc etc

var TodoApp = angular.module("TodoApp", ["HelperModule"]); // <-- Module injected

// TodoApp now contains all the directives/etc from that module!

, !

+2

Source: https://habr.com/ru/post/1544178/


All Articles