0

Im trying to create a directive for a button with a loading state using Bootstrap 3. When the button is disabled I get some strange style which I can't seem to identify. Below I included my code. enter image description here

directive.js

function gfkLoadingButton(settings) {
    return {
        restrict: 'E',
        transclude: true,
        scope: {
            isLoading: "=",
            ngClass: "=",
            ngStyle: "=",
            loadingText: "=",
            ngDisabled: '='
        }
    };
}

template.html

<button class="btn"
        ng-class="ngClass"
        type="button"
        ng-disabled="ngDisabled"
        ng-style="ngStyle">
  <span ng-show="!isLoading">
    <ng-transclude ng-disabled="ngDisabled"></ng-transclude>
  </span>
  <span ng-show="isLoading">
    <i class="fa fa-spinner fa-pulse"></i>
    {{loadingText}}
  </span>
</button>

usage

<loading-button ng-class="'btn-primary'"
                ng-style="{'width': '144px'}"
                ng-disabled="addAdjustmentForm.$invalid || state.saving"
                is-loading="state.saving"
                loading-text="Saving">
    <span class="glyphicon glyphicon-plus"></span>
    Add Adjustment
</loading-button>
Thijs
  • 3,015
  • 4
  • 29
  • 54

2 Answers2

0

ngDisabled is an expression so it should be ngDisabled: '&' instead of '=', and in your template it should be ng-disabled = "ngDisabled()".

Xnake
  • 940
  • 7
  • 8
0

Someone pointed the solution out in the comments but deleted their comment shortly after. Either way, thank you unknown commenter, the solution was really simple. I just needed to include replace: true to the code.

directive.js

function loadingButton(settings) {
    return {
        restrict: 'E',
        transclude: true,
        replace: true,
        scope: {
            isLoading: "=",
            loadingText: "=",
            ngDisabled: '='
        }
    };
}

directive.html

<button class="btn"
        type="button">
  <span ng-show="!isLoading">
    <ng-transclude ng-disabled="ngDisabled"></ng-transclude>
  </span>
  <span ng-show="isLoading">
    <i class="fa fa-spinner fa-pulse"></i>
    {{loadingText}}
  </span>
</button>

usage

<loading-button ng-class="'btn-primary'"
                ng-style="{'width': '143px'}"
                ng-disabled="addCalculationForm.$invalid || state.saving"
                is-loading="state.saving"
                loading-text="'Saving'">
    <span class="glyphicon glyphicon-plus"></span>
    Add Adjustment
</loading-button>
Thijs
  • 3,015
  • 4
  • 29
  • 54