javascript - AngularJS 中字符串中特定字符的绑定(bind)/建模

标签 javascript html angularjs checkbox

我的 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/

相关文章:

javascript - 绕过 iOS 的鼠标悬停事件

jquery - 鼠标悬停时图像发光?

html - 如何在 ng-repeat 过滤器中添加动态列

javascript - 过滤器在状态 ="ACTIVE"上未按预期运行

javascript - 一页angularjs的单独背景图像

javascript - Nokogiri - 查找 javascript 数组内的值

javascript - 如何使用 jasmine 测试使用 setInterval 的 Angular 服务?

javascript - 单击更改顶部位置不执行任何操作

html - 使用Bootstrap/CSS做pinterest风格布局,帖子不会落在上面帖子的正下方

angularjs - Angular UI 网格 : How to get number of total items after filtering