Include an object defined outside the Angular ecosystem in the factory

We have an argument that I hope you guys can help solve the problem.

Let's say that I have a simple factory defined as follows:

angular.module('myModule', [])

.factory('Fact', function() {
    var Fact = function() {
        var that = {};

        that.var1 = [];
        that.var2 = [];

        return that;
    };

    var fact = {
        get: function() {
            return Fact();
        }
    };

    return fact;
});

I found out that this code will work if I write it like this:

angular.module('myModule', [])

.factory('Fact', function() {
    var fact = {
        get: function() {
            return Fact();
        }
    };

    return fact;
});


var Fact = function() {
    var that = {};

    that.var1 = [];
    that.var2 = [];

    return that;
};

Namely, I can take this code of the object Factand put it in some other simple file .js, include it in front of Angular one (in index.html), and this will "work".

However, I am trying to resolve the argument that this approach is not good (well, not the Angular approach), because then this one Fact"lies on a global scale", and this is not good.

In what circumstances would you say this / is not a good solution?

"nonAngular" ( JavaScript, .js ) Angular factory?

+4
1

, , , , Angular , .

, , , .. . , .

, Angular -.

, foojs Angular browserify:

npm

npm install --save foojs

.

var foojs = require('foojs');

var app = angular.app('myapp')

app.service('myservice', function() {
  // here we can use foojs safely
});

WebPack.

, , , .

, .

+2

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


All Articles