So I noticed a pretty weird effect on my bootstrap modals in combination with Angular 4, where the previously opened image pops in first before loading the newly selected image from my gallery. The reason is the value for my details variable is still populated with the old value from the previous (click) event. I can verify this in my console logs. My question is how do I reset this variable in my getArtworkDetail() method before giving it a new value?
I tried setting it like
this.details = undefined;
or
this.details = null;
but it does't seem to register the value at all and instead prevents the modal from loading. My colleague said this is due to the single-threaded nature of TypeScript, but my research on the topic came up short, so I was hoping for some help here. Much obliged!
index.component.html
<!--SEARCH-->
<div class="input-group">
<input #myInput
[(ngModel)]="searchTerm"
type="text"
class="form-control"
placeholder="Search artist or artwork..."
(keyup.enter)="search()"/>
<span class="input-group-btn">
<button #myBtn
(click)="search()"
class="btn btn-default"
type="button">Go!
</button>
</span>
</div>
<!--INDEX-->
<div class="row"
*ngIf="hasArtPieces">
<div *ngFor="let artwork of artworks"
class="col-md-3 col-xs-12 centered">
<div class="card">
<a (click)="getArtworkDetail(artwork.id)"
data-toggle="modal"
data-target="#myModal">
<img class="img-fluid thumb-img"
src="{{artwork.url}}"
alt="Thumbnail">
</a>
<div class="card-block">
<p style="text-align: center"
*ngIf="artwork.title.length < 20; then show else hide">
</p>
<ng-template #show>{{artwork.title }}</ng-template>
<ng-template #hide>{{artwork.title.substring(0,20)}}...</ng-template>
</div>
</div>
<br>
</div>
</div>
<!--DETAIL-->
<div *ngIf="details"
class="modal fade"
id="myModal" tabindex="-1"
role="dialog"
aria-labelledby="myModalLabel">
<div class="modal-dialog"
role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button"
class="close"
data-dismiss="modal"
aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<h1>{{details.title}}</h1>
<h3>{{details.artist}}</h3>
<img class="img-fluid" src="{{details.url}}"
alt=""><br><br>
<p>{{details.description}}</p>
</div>
<div class="modal-footer">
<button type="button"
class="btn btn-default"
data-dismiss="modal">Close
</button>
</div>
</div>
</div>
</div>
index.component.ts
// imports emitted
@Component({
selector: 'artwork-index',
templateUrl: './index.component.html',
styleUrls: ['./index.component.css']
})
export class IndexComponent implements OnInit {
artworks: ArtworkModel[];
searchTerm: string;
details: ArtworkDetailModel;
hasArtPieces = false;
constructor(private detailService: ArtworkDetailsService, private artworksService: ArtworksService) {
}
search() {
if (this.searchTerm) {
this.searchArtworks(this.searchTerm);
}
}
searchArtworks(keyword) {
this.artworksService.searchArtworks(keyword)
.subscribe((artworkData: ArtworkModel[]) => {
this.artworks = artworkData;
this.hasArtPieces = true;
}, err => console.log(err),
() => console.log('Getting artworks complete...'));
}
getArtworkDetail(id) {
this.detailService.getArtworkDetail(id)
.subscribe((detailData: ArtworkDetailModel) => {
this.details = detailData;
}, err => console.log(err),
() => console.log('Getting details complete...'));
}
ngOnInit() {
this.searchArtworks('Rembrandt');
}
}