2
<p ng-repeat="name in names">{{name | cancatSpace}} </p>

app.filter('cancatSpace', function () {
  return function (input) {
      return input.replace(/\s+/g, '');
  };
});

With above code, if my name is 'hello world' it should become helloworld, but it didn't. I don't see any error. Anything else I missed out? I tried to do console.log(input) in my filter it doesn't console anything?

1
  • 1
    Actually it works. Maybe something is wrong in another place, but this filter works for your purpose. Commented Jul 27, 2016 at 3:25

2 Answers 2

1

Whatever code you've posted here is fine. If there's a problem then that would be some minor error. Please see your console and search if there's any error on console.

I've posted the exact code and its working fine. You can run and test it. However, please post your complete code if you want us to debug it.

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

myApp.controller("myCtrl", ['$scope', function($scope) {
  $scope.names = ["john doe", "john doe2", "john doe 3", "john doe  5"];
}]);


myApp
        .filter('cancatSpace', function () {
            return function (input) {
              return input.replace(/\s+/g, '');
            };
        });
        
<!DOCTYPE html>
<html ng-app="myApp">

  <head>
    <link rel="stylesheet" href="style.css">
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.min.js"></script>
    <script src="script.js"></script>
  </head>

  <body ng-controller="myCtrl">
    <p ng-repeat="name in names">{{name | cancatSpace}}</p>
  </body>

</html>

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

Comments

0

try using the following it will concat whatever text you passed in text field.

<div data-ng-app="app" data-ng-controller="mycontroller">
  <input type="text" data-ng-model="greeting" /><br />
  Concated text : {{greeting|concat}}
</div>

<script type="text/javascript">
        var mainApp = angular.module('app', []);

        mainApp.controller('mycontroller', ['$scope', 'concatFilter', function ($scope, concatFilter) {
            $scope.greeting = 'hello hello';
            $scope.concatFilter = concatFilter($scope.greeting);
        }]);

        mainApp.filter('concat', function () {
            return function (input) {
                return input.replace(/\s/g, '');
            };
        });

    </script> 

for more information on filters refer the documentation here..https://docs.angularjs.org/guide/filter

Comments

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.