Fernando Claussen
Fernando Claussen

Reputation: 198

Convert json & to & in AngularJS

I have an HTML element that have value attribute. The value should be Comfort & Protection but the name that comes from the JSON get result is Comfort & Protection and AngularJS is printing it like this on my view.

I try to put this name inside an html element value attribute.

<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

Upvotes: 3

Views: 6439

Answers (2)

Fernando Claussen
Fernando Claussen

Reputation: 198

Managed to solve the problem based on Max Zoom answer.

I created a filter to filter ampersands, like this.

app.filter('ampersand', function(){
    return function(input){
        return input ? input.replace(/&amp;/, '&') : '';
    }
});

And on my View I changed from {{term.name}} to {{term.name | ampersand}}

Upvotes: 2

MaxZoom
MaxZoom

Reputation: 7743

One option would be to replace the &amp; string with & as below:

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

Upvotes: 4

Related Questions