0

Is there any way to get innerHTML code of rendered element that includes ng-repeat loop?

For example:

<div id="container">
    <div ng-repeat="e in ctrl.elements>{{e.name}}</div>
</div>
4
  • 1
    you can use directive Commented Oct 10, 2018 at 11:53
  • 1
    BTW it should be ng-repeat, not ng-for Commented Oct 10, 2018 at 12:04
  • @barbsan Right, sorry Commented Oct 10, 2018 at 12:12
  • you probably want to look at transclusion functions docs.angularjs.org/api/ng/service/… Commented Oct 10, 2018 at 12:18

1 Answer 1

2

You can use $document[0].getElementById("container").innerHTML.
Remember to call it after ng-repeat's job is done - I've delayed it in snippet below.

angular.module('app', [])
  .controller('ExampleController',  function($scope, $document, $timeout) {
    $scope.ctrlelements = [{name: "aaaa"}, {name: "bbbb"}];
    
    console.log($document[0].getElementById("container").innerHTML)
    
    $timeout(function(){
      console.log($document[0].getElementById("container").innerHTML)
    }, 1000)
    });
<script src="//code.angularjs.org/snapshot/angular.min.js"></script>
<body ng-app="app">
  <div ng-controller="ExampleController">
    <div id="container">
    <p ng-repeat="e in ctrlelements track by $index">{{e.name}}</p>
  </div>
  
</div>
</body>

PS. There may be some better way to solve your problem than dealing with innerHTML

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

Comments

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.