【问题标题】:Flag or unflag a checkbox depending on the answer given in dialogs (Angular)根据对话框中给出的答案标记或取消标记复选框(Angular)
【发布时间】:2016-11-02 13:54:26
【问题描述】:

目的:我有一个复选框列表,在标记时显示不同的形式(要编译)。当您尝试取消标记复选框时,复选框会显示一个对话框(角度 - $mddialog),询问您是否真的要删除相应的表单。在用户单击“确认”或“取消”之前,如何防止单个复选框被取消标记?如何通过选择取消来保持标记并通过选择确认取消标记?

这是我的代码:

file.js(包含我的 Angular 模块、控制器等):

... //INSIDE controller 'ctrlOne'
$scope.fromService = dialog.deletionConfirmDialog('Title','Wanna remove?');
...
//OUTSIDE controller 'ctrlOne'
...

...
hello.service('dialog', function($mdDialog) {
   this.deletionConfirmDialog = function(title, cont){
        $mdDialog.show($mdDialog.confirm({
            templateUrl: 'deletionConfirmDialog.html',
            title : title,
            textContent : cont,
            ok : 'Confirm',
            cancel: 'Cancel'
        })).then(function() {
             console.log('confirmed');
             //to be completed
        }, function() {
             console.log('abort');
             //to be completed
        });
    }
}
...

stepOne.html:

...
  <div ng-repeat="item in serv" ng-model="item.active">
      <md-checkbox id="{{item.id}}" aria-label="check" type="checkbox">                      ng-model="item.attivo">
 </div>
...

页面stepOne.html连接到特定的控制器('ctrlOne')

【问题讨论】:

    标签: javascript angularjs dialog modal-dialog angularjs-ng-repeat


    【解决方案1】:

    您可以使用ng-checked 指令。

    你可以这样做:

    您的服务:

    hello.service('dialog', function($mdDialog, $q) {
    var deferred = $q.defer();
       this.deletionConfirmDialog = function(title, cont){
            $mdDialog.show($mdDialog.confirm({
                templateUrl: 'deletionConfirmDialog.html',
                title : title,
                textContent : cont,
                ok : 'Confirm',
                cancel: 'Cancel'
            })).then(function() {
                 console.log('confirmed');
                 //to be completed
    deferred.resolve();
            }, function() {
                 console.log('abort');
                 //to be completed
    deferred.reject();
            });
    
    return deferred.promise;
        }
    }
    

    你的html:

    <div ng-repeat="item in serv" ng-model="item.active">
          <md-checkbox ng-checked="tempFlag" id="{{item.id}}" aria-label="check" type="checkbox"ng-model="item.attivo" ng-change="changeFunc()">
     </div>
    

    然后在你的控制器中:

        if($scope.item.attivo)
          $scope.tempFlag = true
        else
          $scope.tempFlag = false;
    
        $scope.changeFunc = function() {
        dialog.deletionConfirmDialog('some title', 'some content').then(accept, reject)
    
        function accept() {
          $scope.tempFlag = !$scope.tempFlag;
        };
    
        function reject() {
         $scope.item.attivo = !$scope.item.attivo;
        }
    
       }
    

    【讨论】:

      【解决方案2】:

      试试这样的:

      <input onchange="myFunction()" type="checkbox" id="myCheck">
      <script>
      function myFunction()
      {
      alert("You unchecked the box!");
      }
      </script>
      

      您还可以对确认按钮使用 onclick 事件,该事件将说明哪些框未选中并告诉用户查看他们的选择。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-12-25
        • 2011-04-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-05-20
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多