2016-12-28 41 views
1

我是角度js中的新角色。當我點擊該複選框時,我有一個複選框會出現確認消息。只有當我確認確認信息時,我才需要選中或取消選中複選框。我怎樣才能做到這一點與角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'); 
      }) 
     }} 
+0

修改changeStatus函數中的x.status值。 – SaiUnique

回答

1

試試這個,並作出適當的改變,改變ng-checkedx.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>

+0

在檢查/取消選中複選框之前,我需要確認消息。現在它發生在點擊事件本身 – Science

+0

你可以使用ng-click ins-ng-change @Science – byteC0de

+0

但是我會在哪裏寫確認信息?@Jithin Raju – Science

0

剛剛成立NG-模型價值爲pe你的邏輯,它應該工作 $ scope.checkValue = 1;如果data.success == 1

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

您的代碼中存在語法問題。將<%x.id%>更改爲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)">