Selecting first radio button in ng-repeat, validation help.

22 views
Skip to first unread message

RLWS

unread,
Aug 22, 2016, 10:12:23 AM8/22/16
to AngularJS
I've used the index below to select the first value in the result set (see yellow highlight), if it's not already selected.

HTML
<li ng-repeat="address in addresses | filter:vm.addressSearch">
<div class="checkbox">
<input type="radio" name="address-list" id="address-{{address.addressId}}"
ng-click="vm.setAddress( address.addressId )"
ng-checked="address.addressId === vm.cart.addressId || $index==0?true:false">
</div>
</li>

JS Validation
vm.setAddress = function( addressId ) {
vm.cart.addressId = addressId;
 updateShippingValid();
};

function updateShippingValid() {
       vm.shippingValid = angular.isNumber( vm.cart.addressId ) && ( vm.cart.addressId !== 0 ) && angular.isString( vm.cart.shipMethod );
}

Now the first addresses radio button will be selected on load, but my validation does not detect it as selected, it has to be clicked again to go through.

Is there a reason for this?

Thanks

Sander Elias

unread,
Aug 23, 2016, 2:31:40 AM8/23/16
to AngularJS
Hi Rlws,

Yes, ngChecked is setting the checked mark, it's not executing your ngClick. You better make sure there is a sensible default in vm.cart.addressId from withing your controller/service

Regards
Sander


Reply all
Reply to author
Forward
0 new messages