Load Angular image

1

I'm passing a project made in Typescript to angular (it's a chess). The problem I have is that I do not know how I can load the images of the chips.

Code that I have to load the cards;

colocarFichas(casilla) : void{
let img = document.createElement("img");
let nombre: string;
    if(casilla.id=="0" || casilla.id=="7"){
      img = document.createElement("img");
      img.setAttribute("src","./torreBlanca.png");
      casilla.appendChild(img);
    nombre="torreBlanco";
    }
    if(casilla.id>=8 && casilla.id<=15){
        img = document.createElement("img");
        img.setAttribute("src","./peonBlanco.png");
        casilla.appendChild(img);
        nombre="peonBlanco";
    }
    if(casilla.id=="1" || casilla.id=="6"){
        img = document.createElement("img");
        img.setAttribute("src","./caballoBlanco.png");
        casilla.appendChild(img);
        nombre="caballoBlanco";
    }

And so with all, the casilla element is generated dynamically by code typescript ;

  let tablero = document.getElementById("tablero");
  let nCasillas = 8*8;
for(let i = 0; i<nCasillas; i++){

  if(i%8 ==0){
      var fila = document.createElement("div");
      fila.className="fila";
      this.cambioColor=!this.cambioColor;
  }
  let casilla = document.createElement("div");
  //Evento del click
   casilla.onclick= new function(){
      let idCasillaClicada=casilla.id;

  }
  //Fin evento click
  casilla.id =i.toString();
  casilla.className="casilla";
  fila.appendChild(casilla);
  tablero.appendChild(fila);

  if(this.cambioColor){
  casilla.style.background= "#233472";
  this.cambioColor=false;
  }else{
    this.cambioColor=true;
   }

  this.colocarFichas(casilla);
}//Fin del For
}// Fin del metodo pintar tablero.

The images are housed in the same directory as the module, so ./nombre I think should work.

Another thing that I think I do not do well is the assignment of the event click on the box casilla.onclick= new function(){

This is the current result that the browser shows me;

    
asked by Hector Lopez 28.05.2018 в 10:35
source

1 answer

1

When you work with Angular, the best thing you can do is follow your rules or everything becomes more complex: since everything is redirected from the URL set as the base (see HTML tag < a href="https://developer.mozilla.org/en/docs/Web/HTML/Elemento/base"> <base> that you probably have in your index.html), you have to think about that URL when you are looking for an image, so I recommend that you move all the images to the assets folder of your project and use something like:

<img class="logo" src="assets/img/mi-imagen.png ">

If the images do not put them there, think that Angular is compiled when you want to go into production, creating a single .js file with all the components and their templates, so I do not know how the compiler would treat the images that were found in the folders of those people.

On the other hand, you should not need to use document.createElement... in your component either, the templates (the views ) allow you to define all the HTML to show in a clean way, leaving the code of the component Typescript to define the model .

    
answered by 28.05.2018 / 12:43
source