angular-filter icon indicating copy to clipboard operation
angular-filter copied to clipboard

"First" filter fails with expressions

Open thisischrisj opened this issue 9 years ago • 6 comments

Given this data:

$scope.types = [
    {
        "name": "type1",
        "default": false
    },
    {
       "name": "type2",
       "default": true
    }
];

This failed: <div type="{{ (types | first: 'default === true').name }}"></div>

I had to revert to: <div type="{{ ((types | where: {default:true}) | first).name }}"></div>

As a test I also did: <div type="{{ (types | first: 1).name }}"></div> but didn't seem to work either

This was working on 0.4.5 but not in 0.5.2

thisischrisj avatar Jan 18 '15 15:01 thisischrisj

Can you please provide some jsbin/fiddle example, @thisischrisj ?

a8m avatar Jan 18 '15 16:01 a8m

Here you go

0.4.4: http://jsfiddle.net/jg1k73xm/1/ 0.5.2: http://jsfiddle.net/jg1k73xm/2/

I know I mentioned v0.4.5 but I couldn't link to the src. I checked with 0.4.6 but got the same result as 0.5.2

thisischrisj avatar Jan 18 '15 20:01 thisischrisj

Thanks @thisischrisj, It's been changed after this discussion: https://github.com/a8m/angular-filter/issues/19#issuecomment-55025048 Thoughts?

a8m avatar Jan 18 '15 22:01 a8m

I can see the logic and my second example shows that reasonable workarounds exist. I would just recommend updating the documentation as this still references the ability to use expressions with first, last, etc...

It probably wouldn't hurt to mention your decision on "only filter without an arguments return an object" too. This would help users understand expected return types.

thisischrisj avatar Jan 18 '15 22:01 thisischrisj

Awesome @thisischrisj, thanks! Feel free to PR.

a8m avatar Jan 18 '15 22:01 a8m

While 'first' seems to be fixed, the same problem seems to apply to 'last' when it's an array of objects not a simple array. Using version 0.5.17.

HugCoder avatar Sep 29 '18 19:09 HugCoder