If you're looking for an alternative to creating a custom directive and integrating with ngModel modelValue/viewValue hooks, you can implement a simple watch in your controller as suggested in the comments. While I'm making assumptions about your JS code since you haven't provided it, here's a basic idea. I prefer using controller assignments over $scope variables (following the Dot Rule).
app.controller("someController", function($scope)
{
var self = this;
this.somecondition = false;
this.bound_data = "testing<br />123";
$scope.$watch(function() { return self.somecondition; }, function(newVal, oldVal)
{
if (newVal)
{
self.bound_data = self.bound_data.replace("<br />", "\n");
}
else
{
self.bound_data = self.bound_data.replace("\n", "<br />");
}
}
});
In my opinion, it's best to store data with NewLines and create a custom Filter that converts NewLines into <br />
tags when displaying outside the textarea. However, if your stored data already includes <br />
tags, using a Watch or Custom Directive with ngModel would be more appropriate. Remember to apply the same transformation when saving the data with <br />
tags by resetting the somecondition
variable back to its default value to trigger the Watch transformation automatically:
self.save = function()
{
// triggers the watch transformation
self.somecondition = false;
// save the data while preserving the <br /> tags
}