我从我的数据库加载具有以下权限的用户:
this.current = {
firstname: "Jack",
lastname: "Sparrow",
permissions: {
0: {id: 3, name: "Perm3"},
1: {id: 1, name: "Perm1"}
}
};
我还有完整的权限列表:
this.permissions = {
0: {id: 1, name: "Perm1"},
1: {id: 2, name: "Perm2"},
2: {id: 3, name: "Perm3"},
3: {id: 4, name: "Perm4"}
};
我想使用复选框来管理用户的权限:
checked
时,权限已添加到current.permissions
!checked
时,权限已从current.permissions
每个权限都是唯一的,不能在current.permissions
我写了这段代码,我想我应该使用ng-checked
指令,但我不知道如何......
<p>{{current.firstname}} {{current.lastname}} permissions:</p>
<div ng-repeat="p in permissions">
<input type="checkbox" ng-checked=""></input>{{p.name}}
</div>
你能帮帮我吗?
谢谢! :)
答案 0 :(得分:0)
以下是one possible way。
<强> indexOfById 强>
//if the password is obscured and the toggle to show it has been turned on, display password. Else, obscure it.
- (IBAction)togglePasswordVisibility:(id)sender {
// Xcode contains a bug where the font changes to default font if these two lines of code are not included.
self.passwordInputTextField.font = nil;
self.passwordInputTextField.font = [UIFont fontWithName:@"myCustomFontName" size:myDesiredFontSize]; //set this in viewWillAppear as well!
if (self.passwordInputTextField.secureTextEntry == YES) {
self.passwordInputTextField.secureTextEntry = NO;
[self.showHideButton setTitle:@"HIDE" forState:UIControlStateNormal];
} else {
self.passwordInputTextField.secureTextEntry = YES;
[self.showHideButton setTitle:@"SHOW" forState:UIControlStateNormal];
}
}
函数类似于indexOfById
- 它接受一个权限对象,并在用户的权限数组中返回其索引(如果不存在,则返回-1)。
indexOf
<强> triggerPermission 强>
单击复选框时将调用 Array.prototype.indexOfById = function(obj) {
var idx = -1;
for (var i = 0; i < this.length; i++) {
if (this[i].id === obj.id) {
idx = i;
break;
}
}
return idx;
};
,并使用triggerPermission
确定是否添加或删除权限。
indexOfById
triggerPermission = function(perm) {
console.log(perm.name + ' changed')
var idx = $scope.current.permissions.indexOfById(perm);
if (idx === -1) {
$scope.current.permissions.push(perm);
} else {
console.log('splice! ' + idx);
$scope.current.permissions.splice(idx, 1);
}
};
预先检查用户已拥有的权限,这就是为什么我使用ngChecked
代替ngClick
来触发权限add \ remove({{3 } {} ngChange
需要ngChange
但ngModel
不能与ngChecked
一起使用。)答案 1 :(得分:-1)
编辑
我做了little working solution for you
提供一项功能,将该权限添加到当前用户。
这样的事情:
$scope.addPermission = function(permission) {
if (permission.checked) {
$scope.current.permissions.push(permission)
} else {
var index = $scope.permissions.indexOf(permission);
$scope.current.permissions.splice(index, 1);
}
}
并在您看来:
<div ng-repeat="p in permissions">
<input type="checkbox" ng-model="p.checked" ng-checked="" ng-change="addPermission(p)"></input>{{p.name}}
</div>