Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 0783345

Browse files
committed
feat(directive): add ng:poster for <video>
New directive for proper poster video attr (works like ng-src). Added doc & spec.
1 parent 7566215 commit 0783345

File tree

2 files changed

+46
-2
lines changed

2 files changed

+46
-2
lines changed

src/ng/directive/booleanAttrs.js

+27-2
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,31 @@
108108
* @param {template} ngSrc any string which can contain `{{}}` markup.
109109
*/
110110

111+
/**
112+
* @ngdoc directive
113+
* @name ng.directive:ngPoster
114+
* @restrict A
115+
*
116+
* @description
117+
* Using Angular markup like `{{hash}}` in a `poster` attribute doesn't
118+
* work right: The browser will fetch from the URL with the literal
119+
* text `{{hash}}` until Angular replaces the expression inside
120+
* `{{hash}}`. The `ngPoster` directive solves this problem.
121+
*
122+
* The buggy way to write it:
123+
* <pre>
124+
* <video poster="http://www.gravatar.com/avatar/{{hash}}"></video>
125+
* </pre>
126+
*
127+
* The correct way to write it:
128+
* <pre>
129+
* <video ng-poster="http://www.gravatar.com/avatar/{{hash}}"></video>
130+
* </pre>
131+
*
132+
* @element VIDEO
133+
* @param {template} ngPoster any string which can contain `{{}}` markup.
134+
*/
135+
111136
/**
112137
* @ngdoc directive
113138
* @name ng.directive:ngSrcset
@@ -325,8 +350,8 @@ forEach(BOOLEAN_ATTR, function(propName, attrName) {
325350
});
326351

327352

328-
// ng-src, ng-srcset, ng-href are interpolated
329-
forEach(['src', 'srcset', 'href'], function(attrName) {
353+
// ng-src, ng-srcset, ng-href, ng-poster are interpolated
354+
forEach(['src', 'srcset', 'href', 'poster'], function(attrName) {
330355
var normalized = directiveNormalize('ng-' + attrName);
331356
ngAttributeAliasDirectives[normalized] = function() {
332357
return {

test/ng/directive/booleanAttrsSpec.js

+19
Original file line numberDiff line numberDiff line change
@@ -252,3 +252,22 @@ describe('ngHref', function() {
252252
expect(element.attr('href')).toEqual('http://server');
253253
}));
254254
});
255+
256+
describe("ngPoster", function() {
257+
var element;
258+
259+
afterEach(function() {
260+
dealoc(element);
261+
});
262+
263+
it("should interpolate the expression and bind the poster", inject(function($rootScope, $compile){
264+
element = $compile('<video ng-poster="some/{{poster_img}}"></video>')($rootScope)
265+
$rootScope.$digest();
266+
expect(element.attr('poster')).toEqual('some/');
267+
268+
$rootScope.$apply(function() {
269+
$rootScope.poster_img = 1;
270+
});
271+
expect(element.attr('poster')).toEqual('some/1');
272+
}));
273+
});

0 commit comments

Comments
 (0)