Saturday 15 September 2012

javascript - AngularJs filter:$viewValue is not handling html escaping -



javascript - AngularJs filter:$viewValue is not handling html escaping -

if type in box, you'll see:

alabama missing text in bracket alaska fine arizona displays fine 1 time selected shows < , >

any thought on how prevent filter sanitizing or (best) utilize proper encoding , decoding of html characters?

http://jsfiddle.net/zjpwe/64/

<div class="container"> <div ng-controller="mainctrl" class="row-fluid"> <form class="row-fluid"> <div class="container-fluid"> <input type="text" ng-model="selected" typeahead="state state in states | filter:$viewvalue" /> </div> </form> </div> </div>

...

angular.module('myapp', ['ui.bootstrap']) .controller("mainctrl", function ($scope) { $scope.selected = ''; $scope.states = ['alabama <where this>', 'alaska [this ok]', 'arizona &lt;select me&gt;']; });

one not-so-beautiful way solve issue apply filters sanitize array elements, , unsanitize them when selected.

http://jsfiddle.net/pwu0r81m/

$scope.$watch('selected', function() { $scope.selected = $scope.selected.replace(new regexp('&lt;', 'g'), '<').replace(new regexp('&gt;', 'g'), '>'); }); angular.module('myapp') .filter('sanitizer', function(){ homecoming function (items) { var filtered = []; angular.foreach(items, function(item){ filtered.push(item.replace(new regexp('<', 'g'), '&lt;').replace(new regexp(">", 'g'), "&gt;")); }); homecoming filtered; } });

javascript angularjs

No comments:

Post a Comment