0

in the angular i trying to use filter in angular to replace some string with another word that it can find, i already try many example, i think my code is just missing something, but i can't figure it out. Here is my code :

here is my app.js

    app.filter('filterStatus', function () {
    return function (text) {

        if(text == 1){
            return str = text.replace(/1/g, "Waiting");
        } else if (text == 2) {
            return str = text.replace(/2/g, "On Process");
        } else if (text == 3) {
            return str = text.replace(/3/g, "On The Way");
        } else if (text == 4) {
            return str = text.replace(/4/g, "Delivered");
        } else if (text == 5) {
            return str = text.replace(/5/g, "Expired");
        }
    };
});

i going to replace "1" with "Waiting" word, here is my html page

            <tr ng-repeat-start="siheaders in singleID.siheader">
                <td>{{siheaders.status | filterStatus}}</td>
            </tr>

but it give me these "Error: text.replace is not a function" error when i use firebug to debug it, what am i missed here?

2
  • Is siheaders.status a string? Looks to me like you are expecting a number. Commented Dec 22, 2015 at 2:44
  • Does that variable contain a string or a number? replace() is a string method. What does console.log(typeof text) show you? I notice your if/else structure is comparing the variable to numbers, not strings (though == will return true for "1"==1). Commented Dec 22, 2015 at 2:49

1 Answer 1

1

There's really no need to do any string replacement.

app.filter('filterStatus', function () {
    return function (text) {
        if(text == 1){
            return "Waiting";
        } else if (text == 2) {
            return "On Process";
        } else if (text == 3) {
            return "On The Way";
        } else if (text == 4) {
            return "Delivered";
        } else if (text == 5) {
            return "Expired";
        }
    };
}
Sign up to request clarification or add additional context in comments.

1 Comment

Or create a map to avoid the if elses

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.