1

I want to replace any character as ★ character in my search box becuase in my data array, all my star data are stored like that.

I've added my code below.

I'm very new in Angular. I haven't achieved this yet.

var app = angular.module("myApp", []);

angular.module('myApp')
  .directive('hide-characters', HideCharactersDirective);

function HideCharactersDirective() {
  return {
    restrict: 'A',
    require: ['ngModel'],
    link: function(scope, element, attrs, ctrls) {
      var ngModelController = ctrls[0];

      ngModelController.$formatters.push(function(viewValue) {
        return viewValue.replace(/./g, '*');
      });
    }
  };
}


app.controller("myCtrl", function($scope) {
  $scope.records = [{
      "Name": "Alfreds Futterkiste",
      "Star": "★★★"
    },
    {
      "Name": "Berglunds snabbköp",
      "Star": "★"
    },
    {
      "Name": "Centro comercial Moctezuma",
      "Star": "★★★★"
    },
    {
      "Name": "Ernst Handel",
      "Star": "★★"
    }
  ]
});
<html lang="">

<head>
  <base target="_blank">
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title></title>


  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
</head>

<body ng-app="myApp">


  <table ng-controller="myCtrl" border="1">
    <input hide-characters ng-model="search.Star" type="text">

    <tr ng-repeat="x in records | filter:search">
      <td>{{x.Name}}</td>
      <td>{{x.Star}}</td>
    </tr>
  </table>
</body>

</html>

1
  • so if I type test (4 char) in search box, you want it to get changed to **** (4 stars), right? Commented Apr 18, 2017 at 3:05

1 Answer 1

1

From what I understood, you want to change any character written in the search bar to a .

I would just add a ng-change that gives me value of ng-model and I would replace every character using RegExp (as you tried).

Find working snippet below:

var app = angular.module("myApp", []);

app.controller("myCtrl", function($scope) {
  $scope.changeValue = function(value) {
    $scope.search.Star = value.replace(/./g, '★')
  }

  $scope.records = [{
      "Name": "Alfreds Futterkiste",
      "Star": "★★★"
    },
    {
      "Name": "Berglunds snabbköp",
      "Star": "★"
    },
    {
      "Name": "Centro comercial Moctezuma",
      "Star": "★★★★"
    },
    {
      "Name": "Ernst Handel",
      "Star": "★★"
    }
  ]
});
<html lang="">

<head>
  <base target="_blank">
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title></title>
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
</head>

<body ng-app="myApp" ng-controller="myCtrl">

  <table border="1">
    <input ng-change="changeValue(search.Star)" ng-model="search.Star" type="text">

    <tr ng-repeat="x in records | filter:search">
      <td>{{x.Name}}</td>
      <td>{{x.Star}}</td>
    </tr>
  </table>
</body>

</html>

Sign up to request clarification or add additional context in comments.

1 Comment

@Grcn glad that helped! :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.