I have the following controller:
angular.module("UserRoles")
.controller("UserRolesController", function($http, $scope){
$scope.usersearch = "";
$scope.users = [];
$scope.roles = {
}
$scope.getusers = function(){
$http.get("/api/finduser/"+$scope.usersearch)
.success(function(data){
$scope.users = data;
});
}
// returns ([ [username, userid], ... ])
$scope.getroles = function($event){
var uid = $($event.currentTarget).find("a").data("userid");
$http({
"method": "GET",
"url":"/api/rolesbyuser/",
"params": {"user_dn": uid}
}).success(function(data){
console.log(data);
$scope.roles = data;
});
// returns {ldapuser: ldapusername, direct_roles: [r1, r2], inheritedroles: [ir1ir2]}
};
});
for the following HTML:
<div class="col-lg-3 col-sm-3">
<form ng-submit="getusers()">
<div class="form-group">
<label for="searchf">User Suchen: {{ usersearch }}</label>
<input ng-model="usersearch" type="text" class="form-control"
placeholder="username">
</div>
</form>
<hr/>
<ul class="list-group">
<li class="list-group-item text-center user-item"
ng-repeat="user in users"
ng-click="getroles($event)">
<a href="" data-userid="{{ user[1] }}">
{{ user[0] }}
</a>
</li>
</ul>
</div>
<div class="col-lg-9">
<div class="well text-center" id="result-well">
<h4>{{ roles.ldapuser }}</h4>
</div>
<div class="panel panel-warning">
<div class="panel-heading">
<h3>Direkte Rollen</h3>
</div>
<div class="panel-body">
<div class="list-group">
<div class="list-group-item"
ng-repeat="role in roles.direct_roles track by $index">
{{ role }}
</div>
</div>
</div>
</div>
<div class="panel panel-success">
<div class="panel-heading">
<h3>Vererbte Rollen</h3>
</div>
<div class="panel-body">
<div class="list-group">
<div class="list-group-item"
ng-repeat="role in roles.inherited_roles track by $index">
{{ role }}
</div>
</div>
</div>
</div>
</div>
Everything works fine:
In the form, I search for the user, then I am getting a user list in the format [[user, id] ...]
. The site presents the user name
and the id
is saved in the data
attribute of the user. If one clicks on one of the users on the left side, angular fetches the roles for the according user and displays them.
The questions are now:
My click event looks ugly (I use jQuery). What would be the angular way to perform this?
$($event.currentTarget).find("a").data("userid")
- Is there an even better way to set the user to be searched for on click?
Should I really use only one controller or should I use one for user and one for roles?
$($event.currentTarget).find("a").data("userid")