AngularJs: How to set radio button checked based on model

前端 未结 6 1426
礼貌的吻别
礼貌的吻别 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:17

    As discussed somewhat in the question comments, this is one way you could do it:

    • When you first retrieve the data, loop through all locations and set storeDefault to the store that is currently the default.
    • In the markup:
    • Before you save the data, loop through all the merchant.storeLocations and set isDefault to false except for the store where location.id compares equal to storeDefault.

    The above assumes that each location has a field (e.g., id) that holds a unique value.

    Note that $parent.storeDefault is used because ng-repeat creates a child scope, and we want to manipulate the storeDefault parameter on the parent scope.

    Fiddle.

提交回复
热议问题