AngularJs: How to set radio button checked based on model

前端 未结 6 1412
礼貌的吻别
礼貌的吻别 2020-12-05 04:10

I have a model returning in the storeLocations object with a isDefault value. if isDefault returns true, I wan\'t to set that radio button in the group as checked.

N

6条回答
  •  借酒劲吻你
    2020-12-05 04:26

    One way that I see more powerful and avoid having a isDefault in all the models is by using the ng-attributes ng-model, ng-value and ng-checked.

    ng-model: binds the value to your model.

    ng-value: the value to pass to the ng-model binding.

    ng-checked: value or expression that is evaluated. Useful for radio-button and check-boxes.

    Example of use: In the following example, I have my model and a list of languages that my site supports. To display the different languages supported and updating the model with the selecting language we can do it in this way.

    
    

    Our model site.lang will get a language value whenever the expression under evaluation (site.lang == language) is true. This will allow you to sync it with server easily since your model already has the change.

提交回复
热议问题