Search code examples
angularjsformsradio-button

Get data, change one single element and return to api


I get Data from an api and easily list them, now i want to change one element of those, to "default". I dont know how to control that,

Here is the js.

    $scope.updateDefault = function() {
                if (AuthService.isAuth()) {

                    Account.one().get().then(
                            function(account) {

                              **account.data.defaultMsisdn= $scope.element.isSelected;
                                $log.d("account: ", account.data);
                                account.data.put();**
                            }
                    );
                }
            };

here is the HTML:

     <div id="container" style="width:650px">
    <ol ng-repeat="element in accountmsisdn">

        **<li style=" text-align:left; float:left">
            <input type="radio" ng-model="element.isSelected" ng-click="updateDefault()">
            <span>{{element.msisdn}} </span>
        </li>**
        <li style="text-align:center;float: inside" ng-switch="element.active">
            <span ng-switch-when=true>
                {{'view.settings.sender.active'| translate}}</span>
            <span ng-switch-when=false>
                {{'view.settings.sender.notactive'| translate}}</span>
            <span ng-switch-default>
                <strong> - </strong> </span>
        </li>

Solution

  • You need to pass the current element as an argument to updateDefault(), so it knows what element to update:

    <input type="radio" ng-model="element.isSelected" ng-click="updateDefault(element)" />
    
    $scope.updateDefault = function (element) {
        ...
        account.data.defaultMsisdn = element.isSelected;
        ..
    };