我浏览过类似的问题几个小时,但没有拿出任何完全匹配的东西。我想要做的是有一个复选框通过数据绑定自动检查数据中的真/假值。我可以获取项目名称来加载没有问题,我甚至可以稍后将复选框值保存到服务器,但我无法获得正确加载的初始值。如何让复选框在我的数据模型中绑定到布尔值?
下面是一个简化的例子,显示了基本问题; HTML:
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.6/angular.min.js"></script>
<script src="shoppingListNg.js"></script>
<body ng-app>
<div ng-controller="ShoppingListCtrl">
<table>
<tr ng-repeat="item in shoppingList">
<td>
<div>{{item.text}}</div>
</td>
<td>
<input type="checkbox" ng-model="item.isGotten" />
</td>
</tr>
</table>
</div>
</body>
而这里的Java脚本:
function ShoppingListCtrl($scope) {
$scope.shoppingList = [
{
"text": "V8 fusion",
"isGotten": "true"
},
{
"text": "Whole milk container",
"isGotten": "false"
},
{
"text": "Protein bars",
"isGotten": "true"
}
];
};
任何想法,为什么我不能让这些复选框尊重模型中的“真”与“假”的价值观?当页面加载时,它们始终显示未选中状态。我是否需要使用ng检查或不同的指令?
但是,这不适用于单选按钮你知道如何做单选按钮 – Xvegas 2015-05-14 21:11:54