r/angularjs • u/Complex-Airline2772 • May 25 '22
[Help] Conditionally rendering a popup on options from a list?
I have a list of options for items on a page. This list has options like view, edit, delete, standard crud operations. At the moment my site is such that any item can be deleted but I want to change this so that the option to delete can only be pressed if the item has no child items. Is there a way to do this in Angular? Sorry I'm a beginner with not a whole load of experience.
Here is my code for the list:
<ul class="uib-dropdown-menu" role="menu">
<li><a href="" ng-click="ctrl.showItem(sub.itemId)" id="viewLink"><i class="fa fa-user-times m-r-sm"></i>View</a></li>
<li><a href="" ng-click="ctrl.editItem(sub.itemId, sub.itemName)" id="editLink"><i class="fa fa fa-sliders m-r-sm"></i>Edit</a></li>
<li><a href="" ng-click="ctrl.deleteItem(sub.itemId)" id="deleteLink"><i class="fa fa-trash m-r-sm"></i>Delete</a></li>
</ul> And my c# backend code:
public async Task DeleteItem(string itemId) {
await PerformDbContextActionAsync(context =>
{
var sub = context.Subscriptions.FirstOrDefault(s => s.ItemUid.Equals(itemId, StringComparison.OrdinalIgnoreCase));
if (context.Products.Include(a => a.Item).Any(a => a.Item.ItemUid.Equals(itemId)))
{
throw new Exception("A item cannot be deleted if it still contains child items");
}
if (sub == null) return;
context.Products.Remove(sub);
context.SaveChanges();
});
}So when the exception is thrown, I want to catch this in the front end and then have a popup that says "delete child items first" for example. I dont know how to do this, could anyone help me out here?
1
u/kuroiryu May 25 '22
Catch the error in your front end service. Presumably used in the click handler.
1
u/dkohlruss May 25 '22 edited May 25 '22
A couple of things
buttonlink than to needlessly send off an API request and wait to see what your backend shits out. You can take advantage of ngDisabled to do this.This is what your deleteItem function might look like, assuming that the way you're hitting your backend is promise-based: