Skip to content

Instantly share code, notes, and snippets.

@cointilt
Last active August 29, 2015 14:06
Show Gist options
  • Save cointilt/a1f47c5fd0b5dfdf5d13 to your computer and use it in GitHub Desktop.
Save cointilt/a1f47c5fd0b5dfdf5d13 to your computer and use it in GitHub Desktop.
Define || Assign Angular Module
/**
* Assign or Create Angular Module
*
* The purpose of this is to first get the module if it exists, if not then create it
*/
// NO WORK
var myTestModule = angular.module('myTestModule') || angular.module('myTestModule', []);
// WORKS - with the registeredModules.js file
var myTestModule = angular.module('myTestModule');
// Now lets assign some stuff to it since we have it wether it was already created, or if we created it our selves
myTestModule.direct('myButton', function () {
return {
restrict: 'E',
replace: true,
transclude: true,
template: '<button class="btn" ng-transclude></button>'
}
});
// App
var myApp = angular.module('myApp', [
'myTestModule'
]);
// Bootstrap app to document
angular.element(document).ready(function () {
angular.bootstrap(document, ['myApp']);
});
<!DOCTYPE html>
<my-button>Testing Button Directive</my-button>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<script src="registeredModules.js"></script>
<script src="app.js"></script>
(function (angular, undefined) {
'use strict';
var origMethod = angular.module;
var registered = {};
angular.module = function (name, reqs, configFn) {
reqs = reqs || [];
var module = null;
if (registered[name]) {
module = origMethod(name);
module.requires.push.apply(module.requires, reqs);
} else {
module = origMethod(name, reqs, configFn);
registered[name] = module;
}
return module;
};
})(angular);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment