Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I filter to a single item in an array bing to it via an angular expression in markup?

I have a single element that I want bound to a single item in an array and ng-repeat doesn't seem applicable.

How can I do something like the following to bind to a single item in an array

<p class="bottomline">{{vehicle.Taglines[0].Tagline | $filter:{MarketId:$scope.MarketId}}</p>
like image 346
James Alexander Avatar asked Dec 06 '22 05:12

James Alexander


2 Answers

Could you try this:

{{ (vehicle.Taglines | filter: {MarketId: MarketId})[0]["Tagline"] }}

Note, filter not $filter! And you have missed a bracket after the filter object argument!

like image 200
Nikos Paraskevopoulos Avatar answered Mar 09 '23 00:03

Nikos Paraskevopoulos


I don't think it's possible but you can always write that logic in the Controller (and avoid putting so much logic in the template)

module('yourApp', []).controller(['$scope, $filter', function Controller($scope, $filter){
    $scope.$watch('MarketId', function(marketId) {
        $scope.tagLineFound = $filter('filter')($scope.vehicle.Taglines, marketId)[0];
    });
}]);

HTML

<p class="bottomline">{{tagLineFound.Tagline}}</p>
like image 35
enriquecastl Avatar answered Mar 08 '23 23:03

enriquecastl