0

I am creating a custom filter in angular js this is my html,

<div class="box-body">

<div class="form-group">
    <label>Page-Title:</label>
    <input type="text" required value="" data-ng-model="title" name="page_title" class="form-control" id="" placeholder="Enter Page Title">                     
</div>

<div class="form-group">
    <label>Page-Alias:</label>
    <input type="text" value="@{{ title | replaceSpace}}" name="page_alias" class="form-control" id="" placeholder="Auto-Generated If Left Blank">
</div>

This is my angular js code

var app = angular.module('CustomAngular', []);
app.controller('CustomCtrl', function ($scope) {
    app.filter('replaceSpace', function () {
        return function (input) {
            return input.replace(/ /g, '-').toLowerCase();
        };
    });
});

The filter is not working and also I get error in the console.

Error: [$injector:unpr] http://errors.angularjs.org/1.3.15/$injector/unpr?p0=slugifyFilterProvider%20%3C-%20slugifyFilter
    at Error (<anonymous>)

If I use filter: infront of the filter name I donot get any errors in console but it's still not working.

<input type="text" value="@{{ title | filter:replaceSpace }}" name="page_alias" class="form-control" id="" placeholder="Auto-Generated If Left Blank">
2
  • You should not be defining the filter inside your controller, but outside of it. Commented Apr 22, 2015 at 14:56
  • I have done so but still I am getting error in my console and if I use this , value="@{{ title | filter:replaceSpace }}" in my alias input field I get no error but the code doesn't work i.e the filter is not doing it's work am I doing something wrong with the binding between the two input fields @Sergiu Paraschiv Commented Apr 22, 2015 at 15:16

2 Answers 2

3

You don't put filters inside of a controller, put it after your var app line. It's a separate thing just like controllers / directives / etc.

var app = angular.module('CustomAngular', []);

// Here's where it goes
// Also now any/all controllers can use it!

app.filter('replaceSpace', function () {
    return function (input) {
        return input.replace(/ /g, '-').toLowerCase();
    };
});

app.controller('CustomCtrl', function ($scope) {

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

1 Comment

well now I have the same code as yours but still I am getting error in my console and if I use this , value="@{{ title | filter:replaceSpace }}" in my alias input field I get no error but the code doesn't work i.e the filter is not doing it's work am I doing something wrong with the binding between the two input fields @mcpDESIGNS
2

You should define filter outside of controller and not use filter: as it has different meaning, that's the correct way to use your filter

<input type="text" value="@{{ title | replaceSpace }}" name="page_alias" class="form-control" id="" placeholder="Auto-Generated If Left Blank">

although you should either initialize model by $scope.title = '' or place a check in your filter to run replace only when input is defined, otherwise you will get JS errors

filter: is used to filter the array from model, and when you pass another filter to it it does nothing

3 Comments

if I do so I get error in my console and also the binding between the two input field does not work, the alias input field will have {{ title | replaceSpace }} initially when the page loads . The error I get in my console is: Error: [$interpolate:interr] errors.angularjs.org/1.3.15/$interpolate/……D%7D&p1=TypeError%3A%20Cannot%20call%20method%20'replace'%20of%20undefined at Error (<anonymous>)
looks like title isn't initialised by that time then, either state in controller $scope.title = '' or in your filter check if value is undefined before replacing
thanks a lot update your answer so I can accept you answer as best

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.