Javascript 在angular 1.2中使用ngmodel中的表达式
我正在尝试根据特定条件显示选中或未选中的复选框。例如:Javascript 在angular 1.2中使用ngmodel中的表达式,javascript,html,angularjs,Javascript,Html,Angularjs,我正在尝试根据特定条件显示选中或未选中的复选框。例如: $scope.userRoles = {"grants" : [ "Permission", "View", "Update", "Delete" ]} 在HTML部分,我添加了以下代码: <div ng-repeat="p in userRoles"> <input type="checkbox" ng-model="p.grants.indexOf('Delete') !
$scope.userRoles = {"grants" : [
"Permission",
"View",
"Update",
"Delete"
]}
在HTML部分,我添加了以下代码:
<div ng-repeat="p in userRoles">
<input type="checkbox" ng-model="p.grants.indexOf('Delete') != -1?true:false" ng-change="AddRemovePermission(p,'Delete')" />
</div>
如果我使用ng checked而不是ng模型,那么它可以正常工作,但我不会使用它进行双向绑定。另外,我知道我们不能在ng模型中使用上述表达式。
有人能帮我做这件事吗。唯一的条件是,如果用户拥有授权,则应选中复选框,否则不选中,并且当单击复选框时,应相应地将其更改为选中或取消选中,并添加到userRoles对象中。也不能使用指令
谢谢。问题在于您的型号。您必须将布尔值发送到后端,这可以是一个解决方案: 鉴于:
<div ng-repeat="role in userRoles.grants">
<input type="checkbox" ng-model="role.checked" />
</div>
对于复选框,您需要使用ng模型进行ng更改,然后它将作为双向绑定工作
$scope.userRoles = {"grants" : [
{"permission": "Permission", checked: true },
{"permission": "View", checked: false },
{"permission": "Update", checked: true },
{"permission": "Delete", checked: true }
]}