Converting json to & in AngularJS
I have an HTML element that has a value attribute. The value should be "Comfort" and "Protection", but the name returned from the JSON result is "Comfort" &
Protection and AngularJS prints it as shown on my screen.
I am trying to put this name inside the value attribute of an html element.
<div ng-app="app">
<div ng-controller="ExampleCtrl">
<input type="text" ng-repeat="type in types" value="{{type.name}}" />
</div>
</div>
var app = angular.module("app", []);
app.controller('ExampleCtrl', function($scope){
$scope.types = [
{
id: 1,
name: 'Comfort & Protection'
},
{
id: 2,
name: 'Other Name'
}
]
})
+3
source to share
2 answers
Managed to fix the issue based on Max Zoom's answer.
I created a filter to filter out ampersands for example.
app.filter('ampersand', function(){
return function(input){
return input ? input.replace(/&/, '&') : '';
}
});
And in my view I changed from {{term.name}}
to{{term.name | ampersand}}
+1
source to share