javascript 在 angularjs 中动态添加/删除复选框的选中属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31980694/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 14:42:02  来源:igfitidea点击:

Adding/Removing checked attribute to checkbox dynamiclly in angularjs

javascripthtmlangularjs

提问by iam batman

How can i simply add and remove checked attribute to check-box using angularjs. I've find solution from thisquestion,but it requires Jquery

我如何使用 angularjs 简单地添加和删除选中的属性到复选框。我从这个问题中找到了解决方案,但它需要 Jquery

Is any other way to do this without using Jquery?

有没有其他方法可以在不使用 Jquery 的情况下做到这一点?

Here is what i am tried

这是我尝试过的

<input type="checkbox" id="test"  class="switch__input" checked="{{checkVal}}">
<input type="button" ng-click="test()" value="test">

JS

JS

 module.controller('settingsCtrl',function($scope){
  //for adding
  $scope.checkVal="checked";
  //for removing checkbox
  $scope.test=function(){
   $scope.CheckVal="";
  }
}

But the removing wont work

但删除不起作用

采纳答案by venkat7668

This is Angularjsrecommended way of check and un check checkbox

这是Angularjs推荐的检查和取消复选框的方法

HTML: <input type="checkbox" ng-model="checkVal" ng-true-value="true" ng-false-value="false">

HTML: <input type="checkbox" ng-model="checkVal" ng-true-value="true" ng-false-value="false">

Also works

也有效

<input type="checkbox" ng-checked="checkVal">

JS:

JS

module.controller('settingsCtrl',function($scope){
  //for adding
  $scope.checkVal=true;
  //for removing checkbox
  $scope.test=function(){
   $scope.checkVal=false;
  }
}

回答by dfsq

You don't need special directive for this, ngCheckedwould work well:

您不需要为此提供特殊指令,ngChecked可以很好地工作:

var app = angular.module('demo', []).controller('MainController', function($scope) {
  $scope.checkVal = true;

  $scope.test = function() {
    $scope.checkVal = !$scope.checkVal; // or false
  };
});
<script src="https://code.angularjs.org/1.4.3/angular.js"></script>
<div ng-app="demo" ng-controller="MainController">
    <input type="checkbox" id="test" class="switch__input" ng-checked="checkVal">
    <input type="button" ng-click="test()" value="test">
</div>

回答by User2

Please check working example : DEMO

请检查工作示例:演示

HTML

HTML

<input type="checkbox" id="test" class="switch__input" ng-checked="checkVal">
<input type="button" ng-click="test()" value="test">

Controller:

控制器:

var app = angular.module('plunker', []);

app.controller('MainCtrl', function($scope) {

    $scope.checkVal = true;
    //for adding/removing checkbox
    $scope.test = function() {
           $scope.checkVal = !$scope.checkVal;
    }
});

回答by Hmahwish

I can across a directive to add dynamic attribute may be this can help you

我可以跨一个指令添加动态属性可能这可以帮助你

  myApp.directive('dynAttr', function() {
return {
    scope: { list: '=dynAttr' },
    link: function(scope, elem, attrs){
        for(attr in scope.list){
            elem.attr(scope.list[attr].attr, scope.list[attr].value);   
        }
        //console.log(scope.list);           
    }
};
});

In the controller

在控制器中

$scope.attArray = [
{ attr: 'myattribute', value: '' }
];

Use it as

将其用作

<input dyn-attrs="attArray"></input>