I am dealing with a dropdown menu that displays different groups. Each group has a property (boolean) indicating whether the owner is a user or another group.
If the group's owner is a user, I obtain the ownerID of the group and compare it with an array of users to find the matching user and set that user as the selectedOwner
. On the other hand, if the group's owner is another group, I iterate through all my groups to identify the match and set that group as the selectedOwner
.
This is the function in my controller:
$scope.groupOwner = function (){
var temp = $scope.selectedGroup.ownerIsUser ? $scope.users : $scope.groups;
var index = temp.length;
console.dir(temp);
while(index--){
if($scope.selectedGroup.owner === temp[index].id){
$scope.selectedOwner = temp[index];
console.log($scope.selectedOwner);
break;
};
};
};
Every time the dropdown changes, it triggers the groupOwner
function which examines the selectedUser.ownerIsUser
property to determine which array to search into, users
or groups
.
However, the temp variable always returns true, regardless of what the selectedGroup owner property is set to.
This is how the objects are structured:
User = {
name: Demo Administrator,
id: 90,
domain: i:0#.w|itun\demoadmin_compulite,
email: ,
isAdmin: False
}
selectedGroup = {
name: Test Group,
id: 10,
description: ,
owner: 88,
ownerIsUser: False
}
HTML:
<div class="topRow">
<label for="entityDropDown">Select a Group:</label>
<select id="entityDropDown" ng-model="selectedGroup" ng-options="group as group.name for group in groups" ng-change="getGroupInfo(selectedGroup)"></select>
<div class="delGroupBtn"><a>✖</a>
</div>
</div>
Console output of object:
Object {name: "Test Group 4", id: "117", description: "", owner: "71", ownerIsUser: "False"…}
description: ""
id: "117"
name: "Test Group 4"
owner: "71"
ownerIsUser: "False"
__proto__: Object
Solved:
$scope.groupOwner = function (){
//object stores string not booleans
var isUser = $scope.selectedGroup.ownerIsUser === "True"? true : false;
var owner = isUser ? $scope.user : $scope.group;
var index = owner.length;
console.dir(owner);
while(index--){
if($scope.selectedGroup.owner === owner[index].id){
$scope.selectedOwner = owner[index];
console.log($scope.selectedOwner);
break;
};
};
};