Debojyoti Debojyoti - 7 months ago 35
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, ' ')}}
</h2>


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, '')
}


works

Answer

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

Comments