user964210
user964210

Reputation: 143

Is it possible to get a count of HTML elements in an Angular ng-show expression?

I have a select element that has several option elements that are conditionally added. I would like to do a check and not show the select element at all if there are no option elements within it. Is this possible from within an Angular expression?

<select id="actionsDropDown{{request.id}}" class="reqActions"
        ng-show="(Need to check number of options)"
        ng-model="listing.requestAction[request.id]">
    <option value="EDIT_REQUEST" ng-if="isRequestEditingEnabled(request, listing)">Edit Request</option>
    <option value="RESEND" ng-if="isRequestResendEnabled(request)">Resend</option>
</select>

As you can see, there's nothing in the model to bind the check to. Each ng-if is a mutually exclusive test.

Upvotes: 2

Views: 817

Answers (1)

radyz
radyz

Reputation: 1714

It really depends on your implementation but I assume you have a collection in your controller which you are binding to your select:

$scope.collection = [1, 2, 3..];

So you could do the following:

<select ng-model="model" ng-show="collection.length">
    <option ng-repeat="item in collection" ng-value="item" ng-bind="item"></option>
</select>

What the ng-show directive is doing is hiding the select control when collection.length is falsy. If the collection array has any elements then it will be truthy the it will be shown.

Upvotes: 4

Related Questions