3

I'm fairly new to Angular and am probably missing something obvious but I have the following custom filter:

propertyApp.filter('telLink', function() {
return function(tel) {

    // To avoid interpolating error
    if(typeof(tel) != undefined) {

        // Remove any leading zeros
        if(tel.charAt(0) === '0') {
            tel = tel.substr(1);
        }

        tel = "44" + tel;

        // Remove any internal whiespace
        return tel.replace(" ", "");
    }
}

});

When I use this in a view I get this:

Can't interpolate: tel:{{property.agent_phone | telLink}}
TypeError: Cannot call method 'charAt' of undefined

Could someone point me in the right direction? Thanks in advance.

1 Answer 1

5

Instead of

if(typeof(tel) != undefined) {

just do

if (tel) {

This checks for "truthyness," and an undefined tel will not pass this test (the current code lets undefined values through)

js has lots of strange inconsistent behaviour. Here's the top link on google for truthyness : http://www.sitepoint.com/javascript-truthy-falsy/

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

1 Comment

An even better way would be to use angular.isDefined(tel). It does mostly the same thing but guarantees the value is suitable for angular to consume.

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.