javascript - AngularJS textarea newline on enter key -
i developing angularjs application same how post answers , questions on stackoverflow , live preview of our answers or question below textarea.
here trial code:
index.html-
<div ng-app="bindhtmlexample" ng-controller="examplecontroller"> <textarea ng-model="myhtml"></textarea> <div ng-bind-html="myhtml"></div> </div>
script.js-
(function(angular) { 'use strict'; angular.module('bindhtmlexample', ['ngsanitize']) .controller('examplecontroller', ['$scope', function($scope) { $scope.myhtml = 'i <code>html</code>string ' + '<a href="#">links!</a> , other <em>stuff</em>'; }]); })(window.angular);
when user types in textarea, user can see live preview of he/she typing in <div ng-bind-html="myhtml"></div>
. when user types html code, show real html output.
i have done upto this.
so, problem when user presses enter key
in textarea, newline must started in <div ng-bind-html="myhtml"></div>
.
how do that?
here screenshot of wordpress website, idea want...
thats because newlines in textarea not html line breaks. need replace them this:
$(document).ready(function() { var pad = $('#pad'); var board = $('#board'); pad.keypress(function(event) { board.html(pad.val().replace(/\r?\n/g, '<br />')); }); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <textarea id="pad"></textarea> <div id="board"></div>
Comments
Post a Comment