mirror of
https://github.com/Hopiu/angular.js.git
synced 2026-05-05 13:44:42 +00:00
refactor(injector): removed loadModule/ng:module
- added module property to doc:example
This commit is contained in:
parent
955551141d
commit
8adae2fdf2
7 changed files with 46 additions and 70 deletions
|
|
@ -28,7 +28,7 @@ this.secondMethod = function() {
|
||||||
myController.$inject = ['$location', '$log'];
|
myController.$inject = ['$location', '$log'];
|
||||||
</pre>
|
</pre>
|
||||||
|
|
||||||
<doc:example>
|
<doc:example module="MyServiceModule">
|
||||||
<doc:source>
|
<doc:source>
|
||||||
<script type="text/javascript">
|
<script type="text/javascript">
|
||||||
angular.module.MyServiceModule = ['$provide', function($provide){
|
angular.module.MyServiceModule = ['$provide', function($provide){
|
||||||
|
|
@ -53,7 +53,7 @@ function myController(notifyService) {
|
||||||
myController.$inject = ['notify'];
|
myController.$inject = ['notify'];
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<div ng:controller="myController" ng:module="MyServiceModule">
|
<div ng:controller="myController">
|
||||||
<p>Let's try this simple notify service, injected into the controller...</p>
|
<p>Let's try this simple notify service, injected into the controller...</p>
|
||||||
<input ng:init="message='test'" type="text" ng:model="message" />
|
<input ng:init="message='test'" type="text" ng:model="message" />
|
||||||
<button ng:click="callNotify(message);">NOTIFY</button>
|
<button ng:click="callNotify(message);">NOTIFY</button>
|
||||||
|
|
|
||||||
|
|
@ -16,31 +16,31 @@ filter to manipulate the DOM.
|
||||||
The following sample filter reverses a text string. In addition, it conditionally makes the
|
The following sample filter reverses a text string. In addition, it conditionally makes the
|
||||||
text upper-case and assigns color.
|
text upper-case and assigns color.
|
||||||
|
|
||||||
<doc:example>
|
<doc:example module="MyReverseModule">
|
||||||
<doc:source>
|
<doc:source>
|
||||||
<script type="text/javascript">
|
<script type="text/javascript">
|
||||||
angular.module.MyReverseModule = function MyModule($filterProvider) {
|
angular.module.MyReverseModule = function ($filterProvider) {
|
||||||
$filterProvider.register('reverse', function() {
|
$filterProvider.register('reverse', function() {
|
||||||
return function(input, uppercase) {
|
return function(input, uppercase) {
|
||||||
var out = "";
|
var out = "";
|
||||||
for (var i = 0; i < input.length; i++) {
|
for (var i = 0; i < input.length; i++) {
|
||||||
out = input.charAt(i) + out;
|
out = input.charAt(i) + out;
|
||||||
}
|
}
|
||||||
// conditional based on optional argument
|
// conditional based on optional argument
|
||||||
if (uppercase) {
|
if (uppercase) {
|
||||||
out = out.toUpperCase();
|
out = out.toUpperCase();
|
||||||
}
|
}
|
||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function Ctrl() {
|
function Ctrl() {
|
||||||
this.greeting = 'hello';
|
this.greeting = 'hello';
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<div ng:controller="Ctrl" ng:module="MyReverseModule">
|
<div ng:controller="Ctrl">
|
||||||
<input ng:model="greeting" type="greeting"><br>
|
<input ng:model="greeting" type="greeting"><br>
|
||||||
No filter: {{greeting}}<br>
|
No filter: {{greeting}}<br>
|
||||||
Reverse: {{greeting|reverse}}<br>
|
Reverse: {{greeting|reverse}}<br>
|
||||||
|
|
|
||||||
|
|
@ -15,14 +15,15 @@
|
||||||
var HTML_TEMPLATE =
|
var HTML_TEMPLATE =
|
||||||
'<!doctype html>\n' +
|
'<!doctype html>\n' +
|
||||||
'<html xmlns:ng="http://angularjs.org">\n' +
|
'<html xmlns:ng="http://angularjs.org">\n' +
|
||||||
' <script src="' + angularJsUrl + '" ng:autobind></script>\n' +
|
' <script src="' + angularJsUrl + '" ng:autobind_MODULE_></script>\n' +
|
||||||
' <body>\n' +
|
' <body>\n' +
|
||||||
'_HTML_SOURCE_\n' +
|
'_HTML_SOURCE_\n' +
|
||||||
' </body>\n' +
|
' </body>\n' +
|
||||||
'</html>';
|
'</html>';
|
||||||
|
|
||||||
angular.widget('doc:example', function(element){
|
angular.widget('doc:example', ['$injector', '$element', function($injector, element){
|
||||||
this.descend(true); //compile the example code
|
this.descend(true); //compile the example code
|
||||||
|
var module = element.attr('module');
|
||||||
|
|
||||||
//jQuery find() methods in this widget contain primitive selectors on purpose so that we can use
|
//jQuery find() methods in this widget contain primitive selectors on purpose so that we can use
|
||||||
//jqlite instead. jqlite's find() method currently supports onlt getElementsByTagName!
|
//jqlite instead. jqlite's find() method currently supports onlt getElementsByTagName!
|
||||||
|
|
@ -59,7 +60,10 @@
|
||||||
'</ul>';
|
'</ul>';
|
||||||
var tabs = angular.element(tabHtml);
|
var tabs = angular.element(tabHtml);
|
||||||
|
|
||||||
tabs.find('li').eq(1).find('pre').text(HTML_TEMPLATE.replace('_HTML_SOURCE_', code.html));
|
tabs.find('li').eq(1).find('pre').text(
|
||||||
|
HTML_TEMPLATE.
|
||||||
|
replace('_HTML_SOURCE_', code.html).
|
||||||
|
replace('_MODULE_', (module ? (' ng:module="' + module + '"') : '')));
|
||||||
|
|
||||||
element.html('');
|
element.html('');
|
||||||
element.append(tabs);
|
element.append(tabs);
|
||||||
|
|
@ -76,6 +80,11 @@
|
||||||
alert(e);
|
alert(e);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (module) {
|
||||||
|
$injector.invoke(null, angular.module[module]);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
function jsFiddleButton(jsfiddle) {
|
function jsFiddleButton(jsfiddle) {
|
||||||
if (jsfiddle !== 'false') {
|
if (jsfiddle !== 'false') {
|
||||||
if(jsfiddle === true) {
|
if(jsfiddle === true) {
|
||||||
|
|
@ -100,7 +109,7 @@
|
||||||
'</textarea>' +
|
'</textarea>' +
|
||||||
'<input type="text" name="title" value="AngularJS Live Example">' +
|
'<input type="text" name="title" value="AngularJS Live Example">' +
|
||||||
'<textarea name="html">' +
|
'<textarea name="html">' +
|
||||||
'<script src="' + angularJsUrl + '" ng:autobind></script>\n\n' +
|
'<script src="' + angularJsUrl + '" ng:autobind' + (module ? (' ng:module="' + module + '"') : '') + '></script>\n\n' +
|
||||||
'<!-- AngularJS Example Code: -->\n\n' +
|
'<!-- AngularJS Example Code: -->\n\n' +
|
||||||
fiddleSrc +
|
fiddleSrc +
|
||||||
'</textarea>' +
|
'</textarea>' +
|
||||||
|
|
@ -116,7 +125,7 @@
|
||||||
}
|
}
|
||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
});
|
}]);
|
||||||
|
|
||||||
function indent(text) {
|
function indent(text) {
|
||||||
if (!text) return text;
|
if (!text) return text;
|
||||||
|
|
|
||||||
|
|
@ -140,21 +140,6 @@ function inferInjectionArgs(fn) {
|
||||||
* @return new instance of `Type`.
|
* @return new instance of `Type`.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/**
|
|
||||||
* @ngdoc method
|
|
||||||
* @name angular.module.AUTO.$injector#loadModule
|
|
||||||
* @methodOf angular.module.AUTO.$injector
|
|
||||||
* @description
|
|
||||||
* Load additional modules into the current injector configuration
|
|
||||||
*
|
|
||||||
* @param {Array} modules An array of modules, where module is a:
|
|
||||||
*
|
|
||||||
* - `string`: look up the module function from {@link angular.module} and then treat as `function`.
|
|
||||||
* - `function`: execute the module configuration function using
|
|
||||||
* {@link angular.module.AUTO.$injector#invoke $injector.invoke()}
|
|
||||||
*/
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @ngdoc object
|
* @ngdoc object
|
||||||
|
|
@ -255,15 +240,13 @@ function inferInjectionArgs(fn) {
|
||||||
function createInjector(modulesToLoad, moduleRegistry) {
|
function createInjector(modulesToLoad, moduleRegistry) {
|
||||||
var cache = {},
|
var cache = {},
|
||||||
providerSuffix = 'Provider',
|
providerSuffix = 'Provider',
|
||||||
providerSuffixLength = providerSuffix.length,
|
|
||||||
path = [],
|
path = [],
|
||||||
$injector;
|
$injector;
|
||||||
|
|
||||||
value('$injector', $injector = {
|
value('$injector', $injector = {
|
||||||
get: getService,
|
get: getService,
|
||||||
invoke: invoke,
|
invoke: invoke,
|
||||||
instantiate: instantiate,
|
instantiate: instantiate
|
||||||
loadModule: loadModule
|
|
||||||
});
|
});
|
||||||
value('$provide', {
|
value('$provide', {
|
||||||
service: service,
|
service: service,
|
||||||
|
|
|
||||||
|
|
@ -887,12 +887,3 @@ angularDirective("ng:cloak", function(expression, element) {
|
||||||
element.removeAttr('ng:cloak');
|
element.removeAttr('ng:cloak');
|
||||||
element.removeClass('ng-cloak');
|
element.removeClass('ng-cloak');
|
||||||
});
|
});
|
||||||
|
|
||||||
angularDirective('ng:module', ['$value', '$injector',
|
|
||||||
function(modules, $injector) {
|
|
||||||
forEach(modules.split(','), function(module){
|
|
||||||
if (module = trim(module)) {
|
|
||||||
$injector.loadModule(module);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}]);
|
|
||||||
|
|
|
||||||
|
|
@ -244,6 +244,7 @@ function $CompileProvider(){
|
||||||
elementName = nodeName_(element),
|
elementName = nodeName_(element),
|
||||||
elementNamespace = elementName.indexOf(':') > 0 ? lowercase(elementName).replace(':', '-') : '',
|
elementNamespace = elementName.indexOf(':') > 0 ? lowercase(elementName).replace(':', '-') : '',
|
||||||
template,
|
template,
|
||||||
|
locals = {$element: element},
|
||||||
selfApi = {
|
selfApi = {
|
||||||
compile: bind(self, self.compile),
|
compile: bind(self, self.compile),
|
||||||
descend: function(value){ if(isDefined(value)) descend = value; return descend;},
|
descend: function(value){ if(isDefined(value)) descend = value; return descend;},
|
||||||
|
|
@ -256,7 +257,10 @@ function $CompileProvider(){
|
||||||
if (!widget) {
|
if (!widget) {
|
||||||
if ((widget = self.widgets('@' + name))) {
|
if ((widget = self.widgets('@' + name))) {
|
||||||
element.addClass('ng-attr-widget');
|
element.addClass('ng-attr-widget');
|
||||||
widget = bind(selfApi, widget, value, element);
|
if (isFunction(widget) && !widget.$inject) {
|
||||||
|
widget.$inject = ['$value', '$element'];
|
||||||
|
}
|
||||||
|
locals.$value = value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
@ -264,14 +268,16 @@ function $CompileProvider(){
|
||||||
if ((widget = self.widgets(elementName))) {
|
if ((widget = self.widgets(elementName))) {
|
||||||
if (elementNamespace)
|
if (elementNamespace)
|
||||||
element.addClass('ng-widget');
|
element.addClass('ng-widget');
|
||||||
widget = bind(selfApi, widget, element);
|
if (isFunction(widget) && !widget.$inject) {
|
||||||
|
widget.$inject = ['$element'];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (widget) {
|
if (widget) {
|
||||||
descend = false;
|
descend = false;
|
||||||
directives = false;
|
directives = false;
|
||||||
var parent = element.parent();
|
var parent = element.parent();
|
||||||
template.addLinkFn(widget.call(selfApi, element));
|
template.addLinkFn($injector.invoke(selfApi, widget, locals));
|
||||||
if (parent && parent[0]) {
|
if (parent && parent[0]) {
|
||||||
element = jqLite(parent[0].childNodes[elementIndex]);
|
element = jqLite(parent[0].childNodes[elementIndex]);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -535,17 +535,4 @@ describe("directive", function() {
|
||||||
expect(element.hasClass('bar')).toBe(true);
|
expect(element.hasClass('bar')).toBe(true);
|
||||||
}));
|
}));
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('ng:module', function() {
|
|
||||||
it('should install the modules', inject(function($injector, $compile, $rootScope) {
|
|
||||||
var log = '';
|
|
||||||
var injector = $injector;
|
|
||||||
angular.module.a = function($injector){ log += ($injector == injector) + ';';};
|
|
||||||
angular.module.b = function($injector){ log += ($injector == injector); }
|
|
||||||
$compile('<div ng:module=" a, ,,, b "></div>')($rootScope);
|
|
||||||
expect(log).toEqual('true;true');
|
|
||||||
delete angular.module.a;
|
|
||||||
delete angular.module.b;
|
|
||||||
}));
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue