@@ -136,6 +136,11 @@ var COLLECTION_REPEAT_ATTR_WIDTH_ERROR = "collection-repeat expected attribute c
136
136
var COLLECTION_REPEAT_ATTR_REPEAT_ERROR = "collection-repeat expected expression in form of '_item_ in _collection_[ track by _id_]' but got '%'" ;
137
137
138
138
IonicModule
139
+ . directive ( {
140
+ ngSrc : collectionRepeatSrcDirective ( 'ngSrc' , 'src' ) ,
141
+ ngSrcset : collectionRepeatSrcDirective ( 'ngSrcset' , 'srcset' ) ,
142
+ ngHref : collectionRepeatSrcDirective ( 'ngHref' , 'href' )
143
+ } )
139
144
. directive ( 'collectionRepeat' , [
140
145
'$collectionRepeatManager' ,
141
146
'$collectionDataSource' ,
@@ -230,3 +235,28 @@ function($collectionRepeatManager, $collectionDataSource, $parse) {
230
235
}
231
236
} ;
232
237
} ] ) ;
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