Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ionic 2 /3 ; change image src tag when Onclick

I have ionic page that have an image with 100% width and 100% height, I want to change the image source when I click on it.

this is the html source :

<ion-content>
    <ion-img class="map" src="img/map.jpg" (click)="changeView()"></ion-img>
    <ion-icon class="icon-bonfire" name="ios-bonfire"></ion-icon>
    <ion-icon class="icon-heart" name="md-heart"></ion-icon>
    <ion-icon class="icon-nuclear" name="md-nuclear"></ion-icon>
    <ion-fab top right>
        <button ion-fab color="whitecolor"><ion-icon class="calandaricon" name="md-calendar"></ion-icon></button>
    </ion-fab>
    <ion-fab top left>
        <ion-searchbar> </ion-searchbar>
    </ion-fab>
    <ion-fab bottom right class="fablocate">
        <button ion-fab color="whitecolor"><ion-icon class="locateicon" name="md-locate"></ion-icon></button>
    </ion-fab>
    <ion-fab (click)="ListParrots();" bottom left class="linklist">
        <ion-img class="parrot-list-link" src="img/citydirty.jpg"></ion-img>
    </ion-fab>
</ion-content>

ts source code is below :

import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import {ParrotListPage } from '../parrot-list/parrot-list';

/**
 * Generated class for the MapPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-map',
  templateUrl: 'map.html',
})
export class MapPage {

   images: String[] = Array("img//map.jpg", "img/map1.jpg");

  constructor(public navCtrl: NavController, public navParams: NavParams) {
  }

  changeView(){

  }

  ionViewDidLoad() {
    console.log('ionViewDidLoad MapPage');
  }

  ListParrots(){
    this.navCtrl.push(ParrotListPage);
  }

}

can some one help please to complete all the function? thank !

like image 673
sahnoun Avatar asked Oct 05 '17 16:10

sahnoun


1 Answers

Change your template:

<ion-img class="map" [src]="picToView" (click)="changeView()"></ion-img>

And declare a variable in the class:

picToView:string="img/map.jpg";  // always instatiate with a valid value

And changeView:

  changeView(){
          this.picToView=.....
  }

Regards

Tom

like image 100
TomG Avatar answered Sep 19 '22 23:09

TomG