Angular format interpolation error

I am new to Angular and 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 the following:

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

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

+3
source share
1 answer

Instead

if(typeof(tel) != undefined) {

simply

if (tel) {

This checks for plausibility, and undefined tel fails this test (current code skips undefined values)

js has a lot of weird inconsistent behavior. Here's the top google link for veracity: http://www.sitepoint.com/javascript-truthy-falsy/

+5
source

All Articles