Debojyoti Debojyoti - 9 months ago 45
Javascript Question

In angular js why does this javascript replace method to replace &nbsp works on the controller side but not on the template side

Template angular expression:

<h2 style="white-space:normal">
{{notification.noti_val.replace(/\&nb\s*sp;/g, ' ')}}

does not work

function TodoCtrl($scope) {
$scope.notification = 'ujjal saha&nbsp;share&nbsp;own&nbsp;wall';
$scope.notif = notification.noti_val.replace(/\&nb\s*sp;/g, '')



This does not work because angular expressions are not exactly same as javascript expressions. In your example you are trying to create a regular expression and evaluate it. As mentioned on angular documentation angular expressions do not allow No RegExp Creation With Literal Notation. And since angularjs expressions are forgiving no error is thrown but your code doesn't work silently