1

I wanted to grab the Html DOM inside an element as an string to angular controller. I didn't find good resource online. I have following Html Code:

 <div class="form-group ng-controller="straightRunningBeltsCtrl"">
         <button class="btn btn-success btn-lg" ng-click="post()">Get Service Factor</button>

           <div id="pop-up"><h1>My content here!!</h1></div>
      </div>

And I have following JS code

angular.module('straightRunningBelts', [ ])
.controller('straightRunningBeltsCtrl', straightRunningBeltsCtrl)

function straightRunningBeltsCtrl($stateParams, $scope, $http,  $sce){
 $scope.post= function () {
 var template=$sce.trustAsHtml(angular.element(document.getElementById('pop-up')));//Results an error( $sce.trustAsHtml needs string input)
    }

Variable template needs to get value from DOM. Right now, angular.element(document.getElementById('pop-up') returns object. I wanted to do sometime like JQuery Does by using html() function here. Any help or reference to it is welcomed.

4
  • why no angular.element("#pop-up") ?? Commented Jul 16, 2015 at 14:37
  • @Vanojx1 that results into "Error: [jqLite:nosel] Looking up elements via selectors is not supported by jqLite! See: http://docs.angularjs.org/api/angular.element" error Commented Jul 16, 2015 at 14:39
  • seems that angular.element support the html() function look here at docs.angularjs.org/api/ng/function/angular.element Commented Jul 16, 2015 at 14:46
  • @Vanojx1 I saw that and I am not able to implement it yet! Commented Jul 16, 2015 at 14:49

1 Answer 1

1

To retrieve the HTML inside your DOM element, you can use innerHTML. In your case it would be

document.getElementById('pop-up').innerHTML

Working Plunkr

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

4 Comments

it results into "Error: [$sce:itype] Attempted to trust a non-string value in a content requiring a string: Context: html"!
@DineshDevkota Its working here plnkr.co/edit/XWftIro8DyWnaD0CMwWO?p=preview
Try alerting the template or try checking its value. It is undefined.
Okay, just plain JS worked. I was not thinking straight. Removed $sce.trustAsHtml(angular.element(document.getElementById('pop-up'))) and replaced with document.getElementById('pop-up').innerHTML

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.