问题
I'm using angularjs
1.6
and required to use a specific css
library that will disable a button if the button is defined like:
<button disabled="">
My button
</button>
And will enable the button if defined:
<button disabled="">
My button
</button>
I'm trying to enable/disable it based on the latest value from a property in a service within my controller, but the button is always showing as enabled even if someProperty
is defined or not .. any ideas?
<button {{ ctrl.myService.getData().someProperty ? '' : 'disabled=""' }}>
My button
</button>
回答1:
Try using
<button ng-disabled="ctrl.myService.getData().someProperty ? 'disabled': '' ">
My button
</button>
回答2:
We can achieve this using ng-disabled
attribute. SO it's like:
<button ng-disabled="ctrl.myService.getData().someProperty ? true : false">
My button
</button>
Let me know if it helps.
来源:https://stackoverflow.com/questions/45349719/using-ternary-operator-in-angularjs-to-set-an-attribute-on-an-element