if 문에서 이미지를 동적으로 변경

AngularNewbie

내 html에 내 if 문을 기반으로 한 이미지를 표시하고 싶습니다.

내 Typescript에서 :

styleArray = ["Solitary", "Visual","Auditory","Logical","Physical","Social","Verbal",];
    constructor(){
     for (var i = 0; this.styleArray && i <= this.styleArray.length - 1; i++) {
    if (this.arrayTest[0].style == this.styleArray[i]) {

      this.styles = [
        {
          src: './assets/img/' + this.arrayTest[0].style + '.png',
          /* button: this.styleArray[i] + 'InstructionPage', */
          button: 2
        }
      ];
      console.log("First: " + this.styleArray[i]);
    }
  }


   for(var i=0;  this.styleArray && i <= this.styleArray.length -1 ; i++){
   if (this.arrayTest[1].style == this.styleArray[i]){
     this.styles = [
       { src: './assets/img/'+ this.styleArray[i] + '.png',
         button:3
       }
     ];
     console.log("Second: " +this.styleArray[i]);

   }
  }

  for(var i=0;  this.styleArray && i <= this.styleArray.length -1 ; i++){
   if (this.arrayTest[2].style == this.styleArray[i]){
     this.styles = [
       { src: './assets/img/'+ this.styleArray[i] + '.png',
       button:4
       }
     ];
     console.log("Third: " +this.styleArray[i]);

   }
  }
  for(var i=0;  this.styleArray && i <= this.styleArray.length -1 ; i++){
   if (this.arrayTest[3].style == this.styleArray[i]){
     this.styles = [
       { src: './assets/img/'+ this.styleArray[i] + '.png',
       button:5
       }
     ];
     console.log("Fourth: " +this.styleArray[i]);

   }
  }
  for(var i=0;   this.styleArray && i <= this.styleArray.length -1 ; i++){
   if (this.arrayTest[4].style == this.styleArray[i]){
     this.styles = [
       { src: './assets/img/'+ this.styleArray[i] + '.png',
       button:6
       }
     ];
     console.log("Fifth: " +this.styleArray[i]);

   }
  }
  for(var i=0;  this.styleArray && i <= this.styleArray.length -1 ; i++){
   if (this.arrayTest[5].style == this.styleArray[i]){
     this.styles = [
       { src: './assets/img/'+ this.styleArray[i] + '.png',
       button:7
       }
     ];
     console.log("Sixth: " +this.styleArray[i]);

   }
  }
  for(var i=0;  this.styleArray && i <= this.styleArray.length -1 ; i++){
   if (this.arrayTest[6].style == this.styleArray[i]){
     this.styles = [
       { src: './assets/img/'+ this.styleArray[i] + '.png',
       button:8
       }
     ];
     console.log("Seventh: " +this.styleArray[i]);

   }
  }
    }

내 HTML에서 :

<ion-row>
    <ion-col width-25>
      <ion-card class="card-5 full">
        <img class="list" [src]="styles[0].src" (click)="commonMethod(styles[0].button)" />
      </ion-card>
    </ion-col>
  </ion-row>

  <ion-row>
    <ion-col width-25>
      <ion-card class="card-5 full">
        <img class="list" [src]="styles[1].src" (click)="commonMethod(styles[1].button)" />
      </ion-card>
    </ion-col>
  </ion-row>

   <ion-row>
    <ion-col width-25>
      <ion-card class="card-5 full">
        <img class="list" [src]="styles[1].src" (click)="commonMethod(styles[1].button)" />
      </ion-card>
    </ion-col>
  </ion-row>

  <ion-row>
    <ion-col width-25>
      <ion-card class="card-5 full">
        <img class="list" [src]="styles[2].src" (click)="commonMethod(styles[2].button)" />
      </ion-card>
    </ion-col>
  </ion-row>

  <ion-row>
    <ion-col width-25>
      <ion-card class="card-5 full">
        <img class="list" [src]="styles[3].src" (click)="commonMethod(styles[3].button)" />
      </ion-card>
    </ion-col>
  </ion-row>
  <ion-row>
    <ion-col width-25>
      <ion-card class="card-5 full">
        <img class="list" [src]="styles[4].src" (click)="commonMethod(styles[4].button)" />
      </ion-card>
    </ion-col>
  </ion-row>
  <ion-row>
    <ion-col width-25>
      <ion-card class="card-5 full">
        <img class="list" [src]="styles[5].src" (click)="commonMethod(styles[5].button)" />
      </ion-card>
    </ion-col>
  </ion-row>
  <ion-row>
    <ion-col width-25>
      <ion-card class="card-5 full">
        <img class="list" [src]="styles[6].src" (click)="commonMethod(styles[6].button)" />
      </ion-card>
    </ion-col>
  </ion-row>

예를 들어 : 내가 얻은 스타일은 상위 1에서 상위 7까지 정렬되어 있으며, HTML에서 가장 높은 것에서 가장 낮은 것까지 표시하기 위해 내 뷰에 이미지와 버튼을 표시해야합니다.

"src"& "button"정의되지 않은 오류가 있습니다. - 내가 도대체 ​​뭘 잘못하고있는 겁니까?

라자 모하메드 |

여기서 변경해야 할 점은 거의 없습니다.

  1. 제거 , styleArray 마지막에

  2. 생성자 외부에서 스타일을 선언합니다.

  3. 각도 이미지 [src] 속성을 사용하여 이미지를 표시합니다.

암호

 styleArray = ["Solitary", "Visual","Auditory","Logical","Physical","Social","Verbal"];
    styles = [{ src: './assets/img/defaultAvatar.png',
                  button: 'someMethod1'},
              ];
    constructor(){
        for(var i=0; this.styleArray && i <= this.styleArray.length -1 ; i++){
            if (this.arrayTest[0].style == this.styleArray[i]){
              this.styles = [
                { src: './assets/img/'+ this.styleArray[i] + '.png',
                  button: 'AuditoryInstructionPage'},
              ];
             console.log("First: " + this.styleArray[i]);
            }
       }
    }
commonMethod(methodName){
    // If function name comes with () then remove by string manipulation
    //This code will call your method by string 
    this[methodName](); // Pass params if required
}

HTML

<ion-row *ngFor = "styleElement in styles">
   <ion-col width-25 >
            <ion-card *ngIf = "styleElement && styleElement.src && styleElement.button" class="card-5 full">
      <img class="list" [src]="styleElement.src" (click)="commonMethod(styleElement.button)" />
    </ion-card>
  </ion-col>
</ion-row>

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

화면 크기에 맞게 이미지 경로를 동적으로 변경

분류에서Dev

JS-내 tinymce에서 이미지를 동적으로 변경하는 방법

분류에서Dev

Android의 동적 목록에서 동적으로 이미지 변경

분류에서Dev

이미지보기에서 이미지를 동적으로 변경하는 방법은 무엇입니까?

분류에서Dev

텍스트 문서에서 지속적으로 변경되는 (동적) 문자열을 이미지 경로로 .conkyrc에 삽입하는 방법

분류에서Dev

드롭 다운 선택에 따라 jquery에서 이미지를 동적으로 변경하고 div에 삽입

분류에서Dev

wxpython : 배경 / 전경 이미지를 동적으로 변경

분류에서Dev

소스를 변경하기 위해 이미지를 동적으로 선택하는 문제

분류에서Dev

CoreUI의 CBreadcrumbRouter에서 동적으로 페이지 제목 변경

분류에서Dev

Angular2에서 동적으로 페이지 <Head> 변경

분류에서Dev

NSStringWithFormat에서 변수 수를 동적으로 변경

분류에서Dev

gstreamer 파이프 라인에서 udpsrc를 동적으로 변경

분류에서Dev

Javascript- URL의 마지막 문자열을 기반으로 동적으로 배경 이미지 변경

분류에서Dev

mysql에서 자동 증가를 동적으로 변경

분류에서Dev

JavaFx에서 스테이지 높이를 동적으로 변경하는 방법

분류에서Dev

iOS Swift : 동적으로 변경된 레이블의 왼쪽에 이미지를 배치하는 방법

분류에서Dev

터미널 차원 변경에 대한 매뉴얼 페이지를 동적으로 재 형식화

분류에서Dev

각도 js의 값에 따라 이미지를 동적으로 변경하는 방법

분류에서Dev

HTML, JSON의 이미지 소스를 동적으로 변경

분류에서Dev

이미지 크기를 동적으로 변경

분류에서Dev

PHP를 사용하여 동적으로 이미지 변경

분류에서Dev

이미지를 동적으로 변경할 수 없습니다.

분류에서Dev

Konvajs 이미지 src를 동적으로 변경하는 방법

분류에서Dev

netbeans에서 동적으로 변경되지 않는 시간

분류에서Dev

페이지를 새로 고치지 않고 js.erb에서 부분적으로 이미지 변경

분류에서Dev

QML에서 머티리얼 테마를 동적으로 변경

분류에서Dev

iOS 7에서 UIViewController의보기를 동적으로 변경

분류에서Dev

QTabWidget (PyQt)에서 탭보기를 동적으로 변경

분류에서Dev

컨트롤러에서 시계를 동적으로 변경

Related 관련 기사

  1. 1

    화면 크기에 맞게 이미지 경로를 동적으로 변경

  2. 2

    JS-내 tinymce에서 이미지를 동적으로 변경하는 방법

  3. 3

    Android의 동적 목록에서 동적으로 이미지 변경

  4. 4

    이미지보기에서 이미지를 동적으로 변경하는 방법은 무엇입니까?

  5. 5

    텍스트 문서에서 지속적으로 변경되는 (동적) 문자열을 이미지 경로로 .conkyrc에 삽입하는 방법

  6. 6

    드롭 다운 선택에 따라 jquery에서 이미지를 동적으로 변경하고 div에 삽입

  7. 7

    wxpython : 배경 / 전경 이미지를 동적으로 변경

  8. 8

    소스를 변경하기 위해 이미지를 동적으로 선택하는 문제

  9. 9

    CoreUI의 CBreadcrumbRouter에서 동적으로 페이지 제목 변경

  10. 10

    Angular2에서 동적으로 페이지 <Head> 변경

  11. 11

    NSStringWithFormat에서 변수 수를 동적으로 변경

  12. 12

    gstreamer 파이프 라인에서 udpsrc를 동적으로 변경

  13. 13

    Javascript- URL의 마지막 문자열을 기반으로 동적으로 배경 이미지 변경

  14. 14

    mysql에서 자동 증가를 동적으로 변경

  15. 15

    JavaFx에서 스테이지 높이를 동적으로 변경하는 방법

  16. 16

    iOS Swift : 동적으로 변경된 레이블의 왼쪽에 이미지를 배치하는 방법

  17. 17

    터미널 차원 변경에 대한 매뉴얼 페이지를 동적으로 재 형식화

  18. 18

    각도 js의 값에 따라 이미지를 동적으로 변경하는 방법

  19. 19

    HTML, JSON의 이미지 소스를 동적으로 변경

  20. 20

    이미지 크기를 동적으로 변경

  21. 21

    PHP를 사용하여 동적으로 이미지 변경

  22. 22

    이미지를 동적으로 변경할 수 없습니다.

  23. 23

    Konvajs 이미지 src를 동적으로 변경하는 방법

  24. 24

    netbeans에서 동적으로 변경되지 않는 시간

  25. 25

    페이지를 새로 고치지 않고 js.erb에서 부분적으로 이미지 변경

  26. 26

    QML에서 머티리얼 테마를 동적으로 변경

  27. 27

    iOS 7에서 UIViewController의보기를 동적으로 변경

  28. 28

    QTabWidget (PyQt)에서 탭보기를 동적으로 변경

  29. 29

    컨트롤러에서 시계를 동적으로 변경

뜨겁다태그

보관