Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

227
Visualizações
Passing object reference to Angular JS directive

I am iterating over an array of objects that represent HTML elements. I am trying to pass an object reference (or a copy) to my custom directive inside this loop. For each object in the array, I want to create a custom directive and send the object used in that iteration of the loop to the directive. I cannot seem to make this work without the variable being on the controller scope or polluting the global scope with a bunch of alias variables. This is my controller function.

function decode() {

    var html = '';

    for(var element of $scope.currentPage.elements) {
                
        html += `<pv-${element.type} id="${element.id}" element="element"></pv-${element.type}>`;
    }
            
    var j = $(html);  // Compile will not work unless the same var is used in statements below!
    $('#viewer').html(j);
    $compile(j)($scope);
}

I need to pass each object using the element attribute. I can get this to work by creating a reference to the current object on the scope and passing that but of course the data changes upon the next iteration of the loop. I tried sending it as a JSON string and parsing inside of the directive, but that broke too because of the unexpected quotes.

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda