Last active
August 29, 2015 14:06
-
-
Save cointilt/a1f47c5fd0b5dfdf5d13 to your computer and use it in GitHub Desktop.
Define || Assign Angular Module
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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']); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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