Using ternary operator in angularjs to set an attribute on an element

旧时模样 提交于 2019-12-25 09:29:12

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!