angular.js icon indicating copy to clipboard operation
angular.js copied to clipboard

Subkeys filter returning Cannot read property 'xpto' of undefined

Open klederson opened this issue 13 years ago • 3 comments

Hello All,

I'm having a strang problem, when i try to filter a multidimensional object with a key i'm able to do that without problems when i do this:

$filter('filter')($scope.items, { "my.sub.key" : $item.val() }); 

works pretty well... HOWEVER when i try to abstract it using

var myKey = "my.sub.key"; 
$filter('filter')($scope.items, { myKey : $item.val() }); 

i got this error

TypeError: Cannot read property 'my' of undefined but again

Is this a bug or a miss implementation of mine?

klederson avatar Apr 10 '13 16:04 klederson

Looks like it's trying to read the value of the variable as an object when it should be evaluated as a string.

What if you try to cast the value as a string?

$filter('filter')($scope.items, { string(myKey) : $item.val() });

mattymess avatar Apr 10 '13 16:04 mattymess

Yes ( of course string() does not exists to me i did tried other things )

$filter('filter')($scope.items, { myKey.toString() : $item.val() });

that returns ( as anything i try to add there including if i try to concat of course you cannot do operations in object keys.

Uncaught SyntaxError: Unexpected identifier

klederson avatar Apr 10 '13 18:04 klederson

SOLVED

var myKey = "my.sub.key";
var myFilterObj = {};
myFilterObj[myKey] = $item.val();
$filter('filter')($scope.items, myFilterObj); 

This way it worked ... i think syntax can be improved but this is the solution... a little bit tricky but make sense at all it's not AngularJS problem its a Javascript missunderstanding...

Thankyou all

klederson avatar Apr 10 '13 18:04 klederson