我的 Controller 中的数据如下所示:
$scope.myString = "00011"
有没有一种方法可以为字符串中的每个字符建模复选框,如下所示?
<input type="checkbox" ng-model="myString.charAt(0)" ng-checked="'1'">
<input type="checkbox" ng-model="myString.charAt(1)" ng-checked="'1'">
<input type="checkbox" ng-model="myString.charAt(2)" ng-checked="'1'">
<input type="checkbox" ng-model="myString.charAt(3)" ng-checked="'1'">
最终结果,我想要myString
保存我检查过的 1 和 0 的字符串。我上面的示例不起作用,但表明我已尝试解决该问题。
最佳答案
你可以做到这一点,请引用下面的代码片段。
function TodoCtrl($scope) {
$scope.myString = "00011";
$scope.checkBoxModel = Array.from($scope.myString);
$scope.check = function(){
$scope.myString = $scope.checkBoxModel.join("");
console.log( $scope.myString);
}
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app>
<h2>Todo</h2>
<div ng-controller="TodoCtrl">
<input type="checkbox" ng-true-value=1 ng-false-value=0 ng-change="check()" ng-model="checkBoxModel[0]">
<input type="checkbox" ng-true-value=1 ng-false-value=0 ng-change="check()" ng-model="checkBoxModel[1]">
<input type="checkbox" ng-true-value=1 ng-false-value=0 ng-change="check()" ng-model="checkBoxModel[2]">
<input type="checkbox" ng-true-value=1 ng-false-value=0 ng-change="check()" ng-model="checkBoxModel[3]">
</div>
</div>
关于javascript - AngularJS 中字符串中特定字符的绑定(bind)/建模,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41739153/