Search code examples
javascriptangularjsng-classangular-ng-if

more eloquent way to use conditional on buttons for angularjs


right now i'm using an ng-if to determine which button to show my users based on a condition. It just checks if my $scope.method matches the file variable.

<a class="btn btn-default" ng-if="method != file" ng-click="change(file)">Upload</a>
<a class="btn btn-primary" ng-if="method === file" ng-click="change(file)">Upload</a>    

however im wondering if there is a more eloquent way to do this by using ng-class or any other angular directives...


Solution

  • Yes, you are correct. ng-class is what you are wanting.

    <a class="btn" ng-class="{'btn-default': method != file, 'btn-primary': method === file}" 
        ng-if="method != file" ng-click="change(file)">Upload</a>
    

    Here is a fantastic article I always reference when I'm needing to use ng-class.