1

This is my Code

<input type="text" ng-model="data.htmlcode" />
$scope.data={};

In the above html page contains text field. when i enter html content in that text field like<div> <h1> welcome </h1> </div>, that content i want to convert to text otherwise that will be calling to mvc controller like using $http services.

eg:- $http.post("/controller/action?data="+$scope.data)
3
  • Simply do: var yourDataToSend = angular.element('<div/>').html($scope.data).text(); Commented Mar 11, 2016 at 6:54
  • check here stackoverflow.com/questions/17289448/… Commented Mar 11, 2016 at 6:55
  • Hi Jaydeep. Your Code also getting plain text, but i want convert entire text present in textfield . i enter text like <div>hello</div> . Your answer get hello text only Commented Mar 11, 2016 at 9:06

1 Answer 1

1

Are you trying to remove the html from the string in data.htmlcode? This code might help you

function stripHTML(s) {
   var holder = document.createElement('div');
   holder.innerHTML = s;
   return holder.innerText;
}


<div> <h1> welcome </h1> </div> 

//would simply print out welcome

If however, you are trying to escape the html string, like return

&lt;div&gt; &lt;h1&gt; welcome &lt;/h1&gt; &lt;/div&gt;

This function might help you then

function encodeHTML(s) {
   var holder = document.createElement('div');
   holder.innerText = s;
   return holder.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.