我有两个对象具有与用户的关系->角色(一个用户到多个角色)。现在,我正在尝试保存该用户,这是我的代码:
<div class="row">
<br>
<div class="panel-default">
<div class="panel-heading">{{addOrUpdateLabel}}</div>
<div class="panel-body">
<form role="form" class="col-xs-6">
<div class="form-group">
<label for="">Enter Email</label>
<input type="text" class="form-control" ng-model="user.email" ng-disabled="disableEmailField"></input>
<p class="help-block"></p>
</div>
<div class="form-group">
<label for="">Enter Name</label>
<input type="text" class="form-control" ng-model="user.name"></input>
<p class="help-block"></p>
</div>
<div class="form-group">
<label for="">Enter Contact Number</label>
<input type="text" class="form-control" ng-model="user.contact"></input>
<p class="help-block"></p>
</div>
<div class="form-group">
<label for="">Add Role to User</label><br>
<select ng-model="currentRole" ng-options="r for r in rolesArray" style="width:380px; height:30px;">
<option value="">Select role</option></select>
<p class="help-block"></p>
</div>
<div class="form-group" ng-hide="hidePasswordField">
<label for="">Enter Password</label>
<input type="text" class="form-control" ng-model="user.password"></input>
<p class="help-block"></p>
</div>
<div class="form-group">
<button class="btn btn-success" type="submit" ng-click="save(user)" ng-show="showAddButton">Save</button>
<button class="btn btn-success" type="submit" ng-click="update(user)" ng-show="showUpdateButton">Update</button>
<button class="btn btn-primary" type="submit" ng-click="clear()">Clear</button>
<a href="/users/all"><button class="btn btn-danger">Cancel</button></a>
</div>
</form>
</div>
</div>
我有roleArray,其中包含一些预定义的角色,例如:
$scope.rolesArray = ["USER", "ADMIN", "DBA"];
答案 0 :(得分:-1)
将所选角色添加到user
方法中的save
对象中。尽管currentRole
不包含该角色的ID,所以不确定如何获取这些ID。
public save(user): void {
angular.forEach(this.currentRole, (role)=> {
user.roles.push({id: role.id, rolename: role.name});
}
}