4

I have two buttons in my view and I want to disable the Summary button until my view is loaded.

<div class="btn-group" ng-init="showData = 1">
    <button ng-model="showData" type="button" ng-class='{"btn btn-primary": showData == 1, "btn btn-white": showData != 1}' ng-click="showData = 1">Headline</button>
    <button ng-model="showData" type="button" ng-class='{"btn btn-primary":showData == 2, "btn btn-white": showData != 2}'  ng-click="showData = 2; summaryCall()">Summary</button>
</div>

I have a variable $scope.loadMainView = false, this change to true when the response of a Web service is ok, so I want want to disable my button until that variable change to true, but I dont know how to achive that. I was thinking on ng-init for a $scope variable to be initialized as false and then asing that to an ng-disable or something like that but I'm not sure, Im new in angular and maybe my approach is not at all correct.

Some help will be great.

2
  • 1
    Setting its initial value to false and changing it when receiving response from your service is a good way of handling this. Commented Jun 22, 2016 at 19:58
  • But in the view I dont know what to put, ng-init or what? I was thinking on create a $scope variable and set the default value to false until the services response is ok, my problem is on the view that I dont know how to manage that.. Commented Jun 22, 2016 at 20:02

2 Answers 2

3

Use ng-disabled

<button ng-disabled="!loadMainView" type="button"></button>

This directive sets the disabled attribute on the element if the expression inside ngDisabled evaluates to truthy.

https://docs.angularjs.org/api/ng/directive/ngDisabled

Sign up to request clarification or add additional context in comments.

2 Comments

Hi Richard, thanks for your time, one more question, what if I want to evaluate to variales, like this? ng-disabled="!loadMainView || !loadSecondaryView Is that correct?
ng-disabled simply evaluates the subsequent expression. In this case it would mean only one of those two would have to be correct
2

using the ngDisabled directive is the right way to go

<button ng-disabled="!loadMainView" type="button"></button>

1 Comment

Thanks for your time :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.