Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

224
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!