Implementing Various Conditions in ng-if Using AngularJS

I have a scenario in my AngularJS application where I need to display different buttons based on the value of type. If type === 'await_otp', then I should display two buttons (resend OTP and cancel), if type === 'submitted', then only the cancel button should be shown. If neither condition is met, no buttons should be displayed.

In the past, I had separate buttons for each condition but now I want to use a single function to handle this logic.

Could someone guide me on how to achieve this?

<ion-footer-bar style="height:auto">
    <div ng-if="vm.canShowCancel()" class="bar bar-footer bar-assertive" style="position: absolute;" ng-click="vm.cancelApplication(vm.applicationDetails.id)">
       <div class="title" translate>CANCELAPPLICATION</div>
    </div>
    <div class="bar" ng-if="!vm.canShowCancel()" style="position:absolute; bottom:0;text-align:center;padding:0 !important" >
      <button style="min-width:50%; border-radius:0px" class="button button-balanced" ng-click="vm.resendOtp(vm.applicationDetails.id)"
              translate>RESEND</button>
      <button style="min-width:50%; border-radius:0px" class="button button-assertive" ng-click="vm.cancelApplication(vm.applicationDetails.id)" translate>CANCELAPPLICATION</button>
   </div>
 </ion-footer-bar>

Controller:

function canShowCancel () {
  if (vm.applicationDetails && (vm.applicationDetails.state === 'submitted' || vm.applicationDetails.state === 'await_otp_verif')) {
    return true;
  }
}

Previous functions used:

function isAwaitingOtp () {
  return vm.applicationDetails && vm.applicationDetails.state === 'await_otp_verif';
}

function isSubmitted () {
  return vm.applicationDetails && vm.applicationDetails.state === 'submitted';
}

Answer №1

To simplify your code, consider using a single function and passing the desired condition as a parameter.

Here is an implementation example:

function checkCondition(conditionType: string){
     return vm.applicationDetails && vm.applicationDetails.state === conditonType;
}

Then you can utilize it in your template like this:

<div *ngIf="checkCondition('await_otp_verif')">... </div>

Answer №2

To distinguish between different scenarios, you can use a single condition based on the access type.

function checkAccess(type: string) {
  if (type === 'admin' && userInfo.role === 'admin') {
    return true;
  } else if (type === 'user' && userInfo.role === 'user') {
    return true;
  } else {
    return false;
  }
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Two states each offering a distinct perspective

I am currently working on modularizing my application using angular-ui-router to create a website with two states: main and checkout. In the main state, I want to have multiple "section" tags defined as ui-view items. However, it seems like there is an iss ...

Which components of node modules are essential for both production and development environments?

Is it safe to delete unnecessary folders within a node modules library, leaving only the essential min files required for my project? For example, when using the angular ui-select library for dropdowns. I am currently only utilizing the min files: node_m ...

Creating new components within A-frame

I've been attempting to implement the bubble function into my scene to generate a sphere, but unfortunately nothing is showing up. Even when I try creating a sphere without using the bubble function, it still doesn't appear in the scene. func ...

"Troubleshooting: Issues with jQuery Counter Functionality

Hey there, I'm new to JavaScript and jQuery! I've got this form set up with Bootstrap's disabled class: Note: the 'disabled' class in bootstrap properly disables and enables the button based on conditions. <form action="" met ...

Steps to effectively pass parameters in a function object literal

As a JavaScript beginner utilizing an object literal pattern, I am attempting to pass integers as min and max parameters to a function in order to retrieve a random number for use in another function called "interaction". However, I encountered the error m ...

Having trouble with triggering a Material UI modal from a Material UI AppBar on a Next.js page

As a newcomer to the world of React.js and Next.js, I am encountering difficulties when trying to open a Material UI modal from a Material UI AppBar within a Next.js page. Most of the code I have implemented here is directly copied from the Material UI we ...

Best Practices for Implementing AngularJS Dependency Injection

I am excited to share that I have recently started delving into my very first Angular JS project. As I navigate through this new experience, I want to ensure that I am effectively managing Multiple dependency injection. Any insights, recommendations, or ...

The jQuery datatable offers a convenient date function that allows for date manipulation in milliseconds format

Recently, I have been working on updating the task owner ID using a lookup field selection in my code. The tasks are displayed based on the selected date from a datepicker value under the query in the controller. However, I encountered an issue where the a ...

How to clear route history when navigating away from a tab in Angular using Ionic 4

My workflow allows users to edit song information. When a user is on a song page, they can simply click the edit button to access the editing page. For instance, if a user is browsing a submission and wishes to make edits, they would transition from the re ...

What is the process for obtaining Style.css, additional CSS, and JavaScript files from a live single page website?

I am currently facing a challenge with a live single page website where I need to make some fixes. Unfortunately, I am unable to access the Style.css file, along with other css and javascript files. Although I managed to save the html file by using Ctrl+s, ...

Autocomplete HTML feature in WebStorm for AngularJS developers

How can I configure WebStorm to provide autocompletion for "Controllers.My" and "c1.test"? // TypeScript code module Controllers { export class My { test: string; constructor() { ...

Restangular adding newly created models to collection following a POST request

When adding a model to a restangular collection using post method, I encountered an issue. var collectionService = restAngular.all('collection'); var collection = collectionService.getList(); var item = { title : "A title" }; collection.pos ...

Issue with jQuery Ajax file upload in CodeIgniter

I am attempting to use AJAX to upload a file in the CodeIgniter framework, but I encountered an error message stating 'You did not select a file to upload.' Please review this code: View <form method="POST" action="" enctype="multipart/form- ...

Achieving dynamic height in a parent div with a sticky header using mui-datatables

Here are the settings I've configured for my mui-datatables: const options = { responsive: "standard", pagination: false, tableBodyHeight: '80vh', }; return ( <MUIDataTable title={"ACME Employee ...

I am unable to locate the appropriate TypeScript type for the `displayName` attribute when it is used as a prop for the `type` component

Having trouble finding the correct type as mentioned in the title. After inspecting with DevTools, I have confirmed that every component I am programmatically looking at has Component.type.displayName. For anything that is not an ElementType, the type is a ...

Steps for connecting an external .otf file in p5.js

I'm struggling to connect a custom font file to my p5.js sketch through a URL in order to upload it on codepen.io. Unfortunately, the font is not available on Google Fonts. I attempted to include the URL in the load font function like this: loadFon ...

Is it possible for me to convert my .ejs file to .html in order to make it compatible with Node.js and Express?

I have an index.html file and I wanted to link it to a twitter.ejs page. Unfortunately, my attempts were unsuccessful, and now I am considering changing the extension from ejs to html. However, this approach did not work either. Do .ejs files only work wit ...

Having trouble with React Page crashing when trying to access the component state

I'm attempting to create a side-bar menu that swaps out content in a <main> tag when menu buttons are clicked. However, I'm facing an issue where the page becomes unresponsive and eventually crashes due to React issues while trying to read ...

Do not need to refresh the form

I developed a PHP-based Feedback form that includes a Popup from Foundation 5. <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <style type="text/css"> .auto-style1 { margin-left: 1px; ...

object passed as value to competent parent

I'm facing an issue where I am trying to pass a value to the parent component, but it is returning an object instead of the expected value. Here's what I have: Child Component render() { return ( this.props.data.map((val, idx) => { ...