Javascript 在angularjs中动态添加/删除复选框的选中属性
如何使用angularjs简单地向复选框添加和删除选中属性。 我已经从这个问题中找到了解决方案,但它需要Jquery 没有使用Jquery还有其他方法可以做到这一点吗 这就是我所尝试的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"> 但是删除不起作用我可以
<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;
}
});