仅当角度js中的确认消息为真时,才选中/取消选中复选框

时间:2016-12-28 05:54:48

标签: angularjs checkbox

我是角度js的新手。当我点击该复选框时,我会出现一个复选框,会出现一条确认消息。只有在确认确认消息后,我才需要选中或取消选中复选框。我怎么能用角度js。(目前复选框是检查/取消选中即使我取消确认消息)。这是我的复选框元素。

<input type="checkbox" 
    ng-init="checkValue = x.status" 
    ng-model="checkValue" 
    ng-true-value="1" 
    ng-false-value="0" 
    ng-checked="x.status == 1" 
    ng-click="changeStatus(<% x.id %>, 1, checkValue)">

这是我的js部分

    $scope.changeStatus     = function(id, iFlag, checkValue) {    
var confrm          = confirm('Are you sure that you want to do this');
        if (confrm) {
            Data.post('url', param)
            .then(function(data){
                if (data.success == 1)
                    AlertService.setSuccess('Status changed successfully!');
                else 
                    AlertService.setError('Some error occured. Please try again');
            })
        }}

3 个答案:

答案 0 :(得分:1)

尝试此操作,并进行适当的更改,将ng-checked值更改为x.status

var jimApp = angular.module("mainApp",  []);

jimApp.controller('mainCtrl', function($scope){
    $scope.status = true;
    $scope.changeStatus     = function(checkValue) {  
        var r = confirm("Are you sure that you want to do this");
        if (!r) {
            $scope.checkValue = !checkValue;
        }
      }
})
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.5/angular.min.js"></script>

<div ng-app="mainApp" ng-controller="mainCtrl">
  {{checkValue}}
    <input type="checkbox" ng-model="checkValue" ng-checked="checkValue" ng-init="checkValue = status" ng-change="changeStatus(checkValue);">
</div>

答案 1 :(得分:0)

根据您的逻辑设置ng-model值,它应该有效     $ scope.checkValue = 1; if data.success == 1

$scope.checkValue = 0; if data.success != 1

答案 2 :(得分:0)

您的代码中存在语法问题。更改&lt;%x.id%&gt;到x.id

 <input type="checkbox" 
ng-init="checkValue = x.status" 
ng-model="checkValue" 
ng-true-value="1" 
ng-false-value="0" 
ng-checked="x.status == 1" 
ng-click="changeStatus(x.id, 1, checkValue)">