mirror of
https://github.com/Hopiu/angular.js.git
synced 2026-05-04 21:24:42 +00:00
new(directive): added ng:module directive for loading modules
This commit is contained in:
parent
4b35a59c6a
commit
085e3c611f
7 changed files with 58 additions and 27 deletions
|
|
@ -1028,6 +1028,7 @@ function publishExternalAPI(angular){
|
||||||
'copy': copy,
|
'copy': copy,
|
||||||
'extend': extend,
|
'extend': extend,
|
||||||
'equals': equals,
|
'equals': equals,
|
||||||
|
'element': jqLite,
|
||||||
'forEach': forEach,
|
'forEach': forEach,
|
||||||
'injector': function(){ return createInjector(arguments, angularModule); },
|
'injector': function(){ return createInjector(arguments, angularModule); },
|
||||||
'noop':noop,
|
'noop':noop,
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,7 @@
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
publishExternalAPI(angular);
|
|
||||||
|
|
||||||
//try to bind to jquery now so that one can write angular.element().read()
|
//try to bind to jquery now so that one can write angular.element().read()
|
||||||
//but we will rebind on bootstrap again.
|
//but we will rebind on bootstrap again.
|
||||||
bindJQuery();
|
bindJQuery();
|
||||||
|
|
||||||
|
publishExternalAPI(angular);
|
||||||
|
|
|
||||||
|
|
@ -167,24 +167,29 @@ function createInjector(modulesToLoad, moduleRegistry) {
|
||||||
instance = new Constructor();
|
instance = new Constructor();
|
||||||
return invoke(instance, Type, locals) || instance;
|
return invoke(instance, Type, locals) || instance;
|
||||||
};
|
};
|
||||||
|
injector.loadModule = loadModule;
|
||||||
return injector;
|
return injector;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function loadModule(modulesToLoad){
|
||||||
forEach(modulesToLoad, function(module){
|
forEach(isString(modulesToLoad) ? modulesToLoad.split(',') : modulesToLoad, function(module) {
|
||||||
if (isString(module)) {
|
if (isString(module)) {
|
||||||
if (moduleRegistry[module]) {
|
if (moduleRegistry[module = trim(module)]) {
|
||||||
module = moduleRegistry[module];
|
module = moduleRegistry[module];
|
||||||
} else {
|
} else {
|
||||||
throw Error("Module '" + module + "' is not defined!");
|
throw Error("Module '" + module + "' is not defined!");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
if (isFunction(module) || isArray(module)) {
|
||||||
if (isFunction(module) || isArray(module)) {
|
$injector(module);
|
||||||
$injector(module);
|
} else {
|
||||||
} else {
|
assertArgFn(module, 'module');
|
||||||
assertArgFn(module, 'module');
|
}
|
||||||
}
|
});
|
||||||
});
|
}
|
||||||
|
|
||||||
|
|
||||||
|
loadModule(modulesToLoad);
|
||||||
|
|
||||||
// instantiate $eager providers
|
// instantiate $eager providers
|
||||||
// for perf we can't do forEach
|
// for perf we can't do forEach
|
||||||
|
|
|
||||||
|
|
@ -690,8 +690,7 @@ angularDirective("ng:class-odd", ngClass(function(i){return i % 2 === 0;}));
|
||||||
<doc:source>
|
<doc:source>
|
||||||
<ol ng:init="names=['John', 'Mary', 'Cate', 'Suz']">
|
<ol ng:init="names=['John', 'Mary', 'Cate', 'Suz']">
|
||||||
<li ng:repeat="name in names">
|
<li ng:repeat="name in names">
|
||||||
<span ng:class-odd="'ng-format-negative'"
|
<span ng:class-odd="'odd'" ng:class-even="'even'">
|
||||||
ng:class-even="'ng-input-indicator-wait'">
|
|
||||||
{{name}}
|
{{name}}
|
||||||
</span>
|
</span>
|
||||||
</li>
|
</li>
|
||||||
|
|
@ -700,9 +699,9 @@ angularDirective("ng:class-odd", ngClass(function(i){return i % 2 === 0;}));
|
||||||
<doc:scenario>
|
<doc:scenario>
|
||||||
it('should check ng:class-odd and ng:class-even', function() {
|
it('should check ng:class-odd and ng:class-even', function() {
|
||||||
expect(element('.doc-example-live li:first span').prop('className')).
|
expect(element('.doc-example-live li:first span').prop('className')).
|
||||||
toMatch(/ng-format-negative/);
|
toMatch(/odd/);
|
||||||
expect(element('.doc-example-live li:last span').prop('className')).
|
expect(element('.doc-example-live li:last span').prop('className')).
|
||||||
toMatch(/ng-input-indicator-wait/);
|
toMatch(/even/);
|
||||||
});
|
});
|
||||||
</doc:scenario>
|
</doc:scenario>
|
||||||
</doc:example>
|
</doc:example>
|
||||||
|
|
@ -888,3 +887,12 @@ 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);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}]);
|
||||||
|
|
|
||||||
|
|
@ -27,7 +27,11 @@ function $CompileProvider(){
|
||||||
}
|
}
|
||||||
forEach(this.linkFns, function(fn) {
|
forEach(this.linkFns, function(fn) {
|
||||||
try {
|
try {
|
||||||
$injector.invoke(childScope, fn, locals);
|
if (isArray(fn) || fn.$inject) {
|
||||||
|
$injector.invoke(childScope, fn, locals);
|
||||||
|
} else {
|
||||||
|
fn.call(childScope, element);
|
||||||
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
$exceptionHandler(e);
|
$exceptionHandler(e);
|
||||||
}
|
}
|
||||||
|
|
@ -52,10 +56,6 @@ function $CompileProvider(){
|
||||||
|
|
||||||
addLinkFn:function(linkingFn) {
|
addLinkFn:function(linkingFn) {
|
||||||
if (linkingFn) {
|
if (linkingFn) {
|
||||||
//TODO(misko): temporary hack.
|
|
||||||
if (isFunction(linkingFn) && !linkingFn.$inject) {
|
|
||||||
linkingFn.$inject = ['$element'];
|
|
||||||
}
|
|
||||||
this.linkFns.push(linkingFn);
|
this.linkFns.push(linkingFn);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
@ -298,7 +298,9 @@ function $CompileProvider(){
|
||||||
fn = directiveFns[name];
|
fn = directiveFns[name];
|
||||||
if (fn) {
|
if (fn) {
|
||||||
element.addClass('ng-directive');
|
element.addClass('ng-directive');
|
||||||
template.addLinkFn((directiveFns[name]).call(selfApi, value, element));
|
template.addLinkFn((isArray(fn) || fn.$inject)
|
||||||
|
? $injector.invoke(selfApi, fn, {$value:value, $element: element})
|
||||||
|
: fn.call(selfApi, value, element));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -535,4 +535,17 @@ 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;
|
||||||
|
}));
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -11,6 +11,10 @@ angular.scenario.testing.MockAngular.prototype.reset = function() {
|
||||||
this.log = [];
|
this.log = [];
|
||||||
};
|
};
|
||||||
|
|
||||||
|
angular.scenario.testing.MockAngular.prototype.element = function(e) {
|
||||||
|
return jqLite(e);
|
||||||
|
};
|
||||||
|
|
||||||
angular.scenario.testing.MockAngular.prototype.$browser = function() {
|
angular.scenario.testing.MockAngular.prototype.$browser = function() {
|
||||||
this.log.push('$brower()');
|
this.log.push('$brower()');
|
||||||
return this;
|
return this;
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue