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 &amp; Protection'
        },
        {
            id: 2,
            name: 'Other Name'
        }
    ]
})

      

http://codepen.io/Fclaussen/pen/vOXNbg

+3


source to share


2 answers


One option is to replace the string &amp;

with &

as shown below:



var text = name.replace(/&amp;/g, '&')

      

+4


source


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(/&amp;/, '&') : '';
    }
});

      

And in my view I changed from {{term.name}}

to{{term.name | ampersand}}

+1


source







All Articles