简体   繁体   中英

Unable to reset the radio-button value in angular.js

I have two radio buttons where NO is the deault. Once a user clicks on the YES option I am saving this value and trying to make YES as the checked radio button. But I am unable to edit the default value once user changes their option. Here is my code:

    <body ng-app="mainModule" class="">
<div ng-controller="AdminConfigController">
    <div class="aui-page-panel aui-page-focused aui-page-focused-small" id = "continueDiscovery">
        <div class ="aui-page-panel-inner">
            <section class="aui-page-panel-content" >
                <label>Use User Story template while creating JIRA issue for Product features?</label>
                <div ng-repeat="eval in getEvaluators()">
                    <label class="radio" >
                        <input type="radio" ng-model="cell.evaluator" ng-click ="setConfig(cell.evaluator)" name="evaluatorOptions" value="\{{eval.name}}">\{{eval.name}}
                    </label>
                </div>
                <hr />
                <div>You picked: \{{cell.evaluator}}</div>
            </section>
        </div>
    </div>
</div>
</body>

Controller code:

angular.module('mainModule').controller('AdminConfigController', function($scope, $http){


    $scope.cell = function() {
        return $http.get('/getAdminConfig/').success(function(data){
            template = data[0].userTemplate;

        }).error(function(data){
            console.log('Error: ' + data)
        });
    };

    $scope.cell = {
        evaluator: "NO"
    };

    $scope.updateCell = function (option) {
        $scope.cell = {
            evaluator: option
        };

        console.log($scope.cell)
    }
    $scope.evaluators = [{
        name: "YES"
    }, {
        name: "NO"
    }];
    $scope.getEvaluators = function () {
        return $scope.evaluators;
    };

    $scope.setConfig = function (option) {
        console.log("Config Choice:" + option)

        updateCell(option)

        $scope.option = option
        console.log("Entry Choice"+ $scope.option)

        var data = {
            userTemplate : $scope.option
        }

        $http.delete('/resetConfig/').success(function(data){

        }).error(function(data){
            console.log('Error: ' + data)
        });

        $http.post('/setAdminConfig/' , data).success(function(data){
            console.log(data)
            /*$scope.productDiscoveryData = data;*/
        }).error(function(data){
            console.log('Error: ' + data)
        });

    };
});

So if you look at the controller code I need to change the default value

$scope.cell = {
            evaluator: "NO"
        };

to the option selected by the user.

I Hope this code help you.

 function AdminConfigController ($scope){ $scope.Message = 'Hello Hi !'; $scope.getEvaluators = function () { $scope.evaluators = [{name: "YES"}, {name: "NO"}]; return $scope.evaluators; }; $scope.cell = { evaluator: "NO" }; $scope.updateCell = function (option) { $scope.cell = { evaluator: option }; console.log($scope.cell) } $scope.setConfig = function (option) { console.log("Config Choice:" + option) $scope.updateCell(option); $scope.option = option console.log("Entry Choice"+ $scope.option) var data = { userTemplate : $scope.option } }; } 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.22/angular.min.js"></script> <body ng-app="" class=""> <div ng-controller="AdminConfigController"> {{Message}} <div class="aui-page-panel aui-page-focused aui-page-focused-small" id = "continueDiscovery"> <div class ="aui-page-panel-inner"> <section class="aui-page-panel-content" > <label>Use User Story template while creating JIRA issue for Product features?</label> <div ng-repeat="eval in getEvaluators()"> {{$scope.cell.evaluator}} <label class="radio" > <input type="radio" name="evaluatorOptions" ng-model="cell.evaluator" value={{eval.name}}>\\{{eval.name}} </label> </div> <hr /> <div>You picked: \\{{cell.evaluator}}</div> </section> </div> </div> </div> </body> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM