Skip to content

Commit 208ef13

Browse files
committed
fix(collectionRepeat): patch ngSrc/ngHref to fix a bug with falsy values
Fixes #1674
1 parent b1bcece commit 208ef13

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

Diff for: js/angular/directive/collectionRepeat.js

+30
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,11 @@ var COLLECTION_REPEAT_ATTR_WIDTH_ERROR = "collection-repeat expected attribute c
136136
var COLLECTION_REPEAT_ATTR_REPEAT_ERROR = "collection-repeat expected expression in form of '_item_ in _collection_[ track by _id_]' but got '%'";
137137

138138
IonicModule
139+
.directive({
140+
ngSrc: collectionRepeatSrcDirective('ngSrc', 'src'),
141+
ngSrcset: collectionRepeatSrcDirective('ngSrcset', 'srcset'),
142+
ngHref: collectionRepeatSrcDirective('ngHref', 'href')
143+
})
139144
.directive('collectionRepeat', [
140145
'$collectionRepeatManager',
141146
'$collectionDataSource',
@@ -230,3 +235,28 @@ function($collectionRepeatManager, $collectionDataSource, $parse) {
230235
}
231236
};
232237
}]);
238+
239+
// Fix for #1674
240+
// Problem: if an ngSrc or ngHref expression evaluates to a falsy value, it will
241+
// not erase the previous truthy value of the href.
242+
// In collectionRepeat, we re-use elements from before. So if the ngHref expression
243+
// evaluates to truthy for item 1 and then falsy for item 2, if an element changes
244+
// from representing item 1 to representing item 2, item 2 will still have
245+
// item 1's href value.
246+
// Solution: erase the href or src attribute if ngHref/ngSrc are falsy.
247+
function collectionRepeatSrcDirective(ngAttrName, attrName) {
248+
return [function() {
249+
return {
250+
priority: '99', // it needs to run after the attributes are interpolated
251+
require: '^?collectionRepeat',
252+
link: function(scope, element, attr, collectionRepeatCtrl) {
253+
if (!collectionRepeatCtrl) return;
254+
attr.$observe(ngAttrName, function(value) {
255+
if (!value) {
256+
element.removeAttr(attrName);
257+
}
258+
});
259+
}
260+
};
261+
}];
262+
}

0 commit comments

Comments
 (0)