Jimothey
Jimothey

Reputation: 2434

Angular Interpolation Error when formatting number

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.

Upvotes: 3

Views: 3315

Answers (1)

Dan
Dan

Reputation: 3258

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/

Upvotes: 5

Related Questions