1

How can I display array index value in console window on click of the button which is present inside the carousel component? where the console window is displaying index value twice and at last its redirecting to 1st array index value as 1. Console window should only display the index value with increment of 1 so it starts displaying array index value from.

guide.html

<tr *ngFor="let guide of guides">
  <td>{{guide.Type}}</td>
  <td>{{guide.PageColor}}</td>
  <td>
  	<carousel class="carousel slide" data-interval="false">
	    <slide  *ngFor="let image of images; let i=index ">
	      <p>Image: {{i+1}}</p>
	      <img src="{{image.image}}" alt="First slide"/>
	      <p (click)="idValue(i)">
	       	<input type="file" name="file" id="file" />
	       	<label for="file">{{image.change}}</label>
	      </p>
	     </slide>
  	</carousel>
	</td>
</tr>

guide.component.ts

import { Component } from '@angular/core';
import { CarouselModule } from 'ngx-bootstrap';
@Component({
  templateUrl: './guide.html'
})
export class GuideComponent {
	guides : any = [{ Type: 'Company', PageColor: 'red'},{ Type: 'Company', PageColor: 'Blue'}];

	images:any = [{image :'img1.jpg', change:'change'},{image: 'img2.jpg', change:'add'},{image:'img3.jpg', change:'edit'},{image:'im4.jpg',change:'delete' }];
        
	idValue(i) {
		i=i+1;
		console.log(i);
	}
}

5
  • can u please rephrase the question a bit ? i think i might understood what is the problem but i don't want to post an answer before i am certain. thanks Commented Dec 5, 2017 at 10:44
  • console is printing the index value, everytime it is printed twice and it is always printing 1 at 2nd time Commented Dec 5, 2017 at 10:48
  • so how to avoid it printing 1 in console, if index value is 3 its displaying 3 1st and along with 3 its displaying 1 Commented Dec 5, 2017 at 10:49
  • It can be that the carousel $event is triggered twice try in html: <p (click)="$event, idValue(i)"> in in component: idValue(event, i) { i=i+1; console.log(event, i); } and check the output. Commented Dec 5, 2017 at 11:19
  • No its not working with event thank you Commented Dec 5, 2017 at 11:30

1 Answer 1

2

Here is a simplified version of your code, that works. In two words = simple types (number is our case) copied to click-handler arguments, not send by ref. So every time you got same value from i.

import { Component } from '@angular/core';

@Component({
  selector: 'my-app',
  template: `
  <div  *ngFor="let image of images; let i=index ">
          <p>Image: {{i+1}}</p>

          <p (click)="idValue(i)">
          Button
          </p>
         </div>
  `,
})
export class AppComponent  { 

  images:any = [{image :'img1.jpg', change:'change'},{image: 'img2.jpg', change:'add'},{image:'img3.jpg', change:'edit'},{image:'im4.jpg',change:'delete' }];
  indexArr = new Array(this.images.length + 1);

    idValue(i) {
      if(!this.indexArr[i]) { this.indexArr[i] = 0};
        this.indexArr[i] += 1;
        console.log('index:', i, ' Click number', this.indexArr[i]);
    }

}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for trying but its not displaying any index value in console
Corrected to display index and clicknumber

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.