Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/rest/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在angularjs中动态添加/删除复选框的选中属性_Javascript_Html_Angularjs - Fatal编程技术网

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

Javascript 在angularjs中动态添加/删除复选框的选中属性,javascript,html,angularjs,Javascript,Html,Angularjs,如何使用angularjs简单地向复选框添加和删除选中属性。 我已经从这个问题中找到了解决方案,但它需要Jquery 没有使用Jquery还有其他方法可以做到这一点吗 这就是我所尝试的 <input type="checkbox" id="test" class="switch__input" checked="{{checkVal}}"> <input type="button" ng-click="test()" value="test"> 但是删除不起作用我可以

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

没有使用Jquery还有其他方法可以做到这一点吗

这就是我所尝试的

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

但是删除不起作用

我可以跨指令添加动态属性,这可能会对您有所帮助

  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);           
    }
};
});
在控制器中

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

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

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

HTML

也有效

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

您不需要特别的指示,这样可以很好地工作:

var-app=angular.module('demo',[]).controller('MainController',function($scope){
$scope.checkVal=true;
$scope.test=函数(){
$scope.checkVal=!$scope.checkVal;//或false
};
});

请检查工作示例:

HTML


不使用指令是不行的?看起来你有打字错误。第二个CheckVal是大写的。
<input type="checkbox" ng-checked="checkVal">
module.controller('settingsCtrl',function($scope){
  //for adding
  $scope.checkVal=true;
  //for removing checkbox
  $scope.test=function(){
   $scope.checkVal=false;
  }
}
<input type="checkbox" id="test" class="switch__input" ng-checked="checkVal">
<input type="button" ng-click="test()" value="test">
var app = angular.module('plunker', []);

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

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