feat($compile): allow directives to modify interpolated attributes

A directive can now set/update/remove attribute values even those containing
interpolation during the compile phase and have the new value be picked up
during the compilation.

For example in template:

<div replace-directive some-attr-or-directive="{{originalInterpolationValue}}"></div>

the replace-directive can now replace the value of some-attr-or-directive during compilation
which produces this intermitent template:

<div replace-directive some-attr-or-directive="{{replacedInterpolationValue}}"></div>

or even

<div replace-directive some-attr-or-directive="replacedStaticValue"></div>

as well as

<div replace-directive some-attr-or-directive></div>
This commit is contained in:
Thibault Leruitte
2013-02-26 11:14:27 +01:00
committed by Igor Minar
parent eb53423a41
commit fe8d893b83
2 changed files with 22 additions and 6 deletions

View File

@@ -1503,6 +1503,12 @@ describe('$compile', function() {
expect(attr.$observe('someAttr', observeSpy)).toBe(observeSpy);
};
});
directive('replaceSomeAttr', valueFn({
compile: function(element, attr) {
attr.$set('someAttr', 'bar-{{1+1}}');
expect(element).toBe(attr.$$element);
}
}));
}));
@@ -1544,6 +1550,14 @@ describe('$compile', function() {
}));
it('should allow directive to replace interpolated attributes before attr interpolation compilation', inject(
function($compile, $rootScope) {
element = $compile('<div some-attr="foo-{{1+1}}" replace-some-attr></div>')($rootScope);
$rootScope.$digest();
expect(element.attr('some-attr')).toEqual('bar-2');
}));
it('should call observer of non-interpolated attr through $evalAsync',
inject(function($rootScope, $compile) {
$compile('<div some-attr="nonBound" observer></div>')($rootScope);