Robert Christian
Robert Christian

Reputation: 18310

AngularJS - Conditionally apply an attribute?

Conditionally applying an element is easy - just use ng-show. But what about an attribute?

IE

<div ui-sortable>
  ...
</div> 

needs to be sortable only for admins, so do something like...

<div ng-conditional-attribute=" {'ui-sortable': 'user.isAdmin()'} ">
  ...
</div>

Upvotes: 4

Views: 2527

Answers (3)

Charles Hankey
Charles Hankey

Reputation: 505

Late to the party but it may help someone:

ng-readonly ="" 

will set the attribute if the expression is truthy.

There are a number of other ng attributes that work this way.

Upvotes: 0

charlietfl
charlietfl

Reputation: 171690

Since a lot of directives also work from class you might be able to use ng-class="{'ui-sortable': user.isAdmin()}".

I don't know enough about the angular-ui directives to know what restrictions they have

Upvotes: 1

ProLoser
ProLoser

Reputation: 4616

Create a custom directive or template and switch between the version with sortable attached and the version without.

Upvotes: 2

Related Questions