0

In the below code, I am having a template and based on the IsEdit flag the corresponding view will be rendered. From here, Is there a way to get the angular parsed HTML which is only visible to the view.

e.g.

If IsEdit = false, then I want the rendered HTML as

<div>
   View Mode
</div>

or even

<div ng-show="!IsEdit">
   View Mode
</div>

var app = angular.module("myApp", []);

app.controller("myCtrl", function ($scope) {

  $scope.IsEdit = false;

  $scope.GetTemplate = function () {
    console.log($("#Template").html());
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular.min.js"></script>

<div id="Template" ng-app="myApp" ng-controller="myCtrl">

<div ng-show="IsEdit">
  Edit Mode
</div>
<div ng-show="!IsEdit">
  View Mode
</div>

<button ng-click="GetTemplate()">Get HTML</button>
</div>

1 Answer 1

1

Try this one, we use ng-if to completely removed it in the DOM.

var app = angular.module("myApp", []);

app.controller("myCtrl", function ($scope) {

  $scope.IsEdit = false;

  $scope.GetTemplate = function () {
    angular.element('#Template').contents().filter(function(){ return this.nodeType == 8;}).remove(); // <--- to remove comments
    console.log(angular.element('#Template').html());
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular.min.js"></script>

<div id="Template" ng-app="myApp" ng-controller="myCtrl">

<div ng-if="IsEdit">
  Edit Mode
</div>
<div ng-if="!IsEdit">
  View Mode
</div>

<button ng-click="GetTemplate()">Get HTML</button>
</div>

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.