Skip to content

Commit a34ba6c

Browse files
committed
Merge pull request #3 from ishi/add-observe-to-src-attribute
Fixed:when ngSrc was undefined at the beginning, image wasn't refreshed
2 parents b3fc7eb + b9270e8 commit a34ba6c

File tree

2 files changed

+21
-7
lines changed

2 files changed

+21
-7
lines changed

src/angular-wurfl-image-tailor.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,21 +26,22 @@ angular.module('angular-wurfl-image-tailor', [])
2626
replace: false,
2727
scope:{},
2828
template: function(element, attributes){
29-
if (attributes['ngSrc']) {
29+
if ('ngSrc' in attributes) {
3030
return '<div class="wit"><img ng-src="{{wit_link}}"/></div>';
3131
} else {
3232
return '<div class="wit"><img src="{{wit_link}}"/></div>';
3333
}
3434
},
3535
link: function (scope, element, attributes) {
36-
var srcAName = attributes['ngSrc'] ? 'ngSrc' : 'src';
37-
38-
if(!attributes[srcAName]) {
39-
scope.wit_link = '';
40-
return;
41-
}
36+
var srcAName = 'ngSrc' in attributes ? 'ngSrc' : 'src';
37+
scope.wit_link = '';
4238

4339
attributes.$observe(srcAName, function (src) {
40+
if(!src) {
41+
scope.wit_link = '';
42+
return;
43+
}
44+
4445
var wit_link_pieces = [witUrls.get()];
4546
angular.forEach(attributes['$attr'], function (attr) {
4647
if (attr != 'src' && attr != 'ng-src') {

test/unit/directiveSpec.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,17 @@ describe("Unit: Testing angular directive for WURFL Image Tailor", function() {
6969
expect(elt.html()).to.be.a('string');
7070
expect(elt.html()).to.be.equal('<div class="wit"><img ng-src="//wit1.wurfl.io/http://test.com/image2.jpg" src="//wit1.wurfl.io/http://test.com/image2.jpg"></div>');
7171
});
72+
73+
it('should change when ngSrc directive variable change from undefined', function () {
74+
scope.myUrl = undefined;
75+
var elt = angular.element('<img-wit ng-src="{{myUrl}}"></img-wit>');
76+
compile(elt)(scope);
77+
scope.$digest();
78+
scope.myUrl = sce.trustAsResourceUrl('http://test.com/image2.jpg');
79+
scope.$digest();
80+
81+
expect(elt.html()).to.be.a('string');
82+
expect(elt.html()).to.be.equal('<div class="wit"><img ng-src="//wit.wurfl.io/http://test.com/image2.jpg" src="//wit.wurfl.io/http://test.com/image2.jpg"></div>');
83+
});
84+
7285
});

0 commit comments

Comments
 (0)