We're in the midst of a debate and I'm hoping you can help us reach an agreement.
Imagine I have a basic factory set up like this:
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 discovered that if I rearrange the code this way, it still works:
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;
};
Essentially, I could separate the Fact
object's code and place it in another plain .js
file, include it before the Angular code (in the index.html
file) and it would still function properly.
However, we are debating whether or not this is a good practice because having Fact
on the global scope may not be ideal in terms of best practices.
In what scenarios do you think this solution is or isn't advisable?
Is there a way to incorporate "non-Angular" code (simple JavaScript code written in a separate .js
file) into an Angular factory?