Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why angularjs core need to detect whether module ngLocale has been created?

Tags:

angularjs

In AngularJS source code, there is a section like this, the comment is added by me

try {
    angularModule('ngLocale'); //this detect wether ngLocale has been created
} catch (e) {
    //if not created , then create it here
    angularModule('ngLocale', []).provider('$locale', $LocaleProvider);    
}

I understand how ngLocale works, because you can override default ngLocale's $locale service by using files like

<script src="../src/ngLocale/angular-locale_fr-ca.js"></script>

But this after AngularJS core has been run. Because the exception always throw, literally, why not just remove the detection code, and simply use that instead?

angularModule('ngLocale', []).provider('$locale', $LocaleProvider);
like image 279
Fred Yang Avatar asked Apr 08 '14 04:04

Fred Yang


1 Answers

The reason for this is because angularjs lacks the ability to check for the availability of a module on a graceful manner. So instead of a checkModule() or findModule(), the framework needs to induce a "module loading exception" for the nonexistent module.

ensure module could accept a parameter to tell the system if it is OK to not find a module, and simply return null as a result.

Actually, letting the module name to start with a question mark in the var mod = angular.module("?moduleName") code path (and not when defining a module) would make it a simple oneliner code to ommit exceptions.

like image 122
Peter Aron Zentai Avatar answered Sep 21 '22 21:09

Peter Aron Zentai