Prevent Buttons from Being Clicked While Another is Active in Angular

I am facing a challenge with my Angular functions that enable search features. My goal is to allow only one feature to be selected at a time.

/* Trauma Search functionality */
$scope.traumaSearch = false;
$scope.traumaText = "Trauma Center";
$scope.toggleTraumaSearch = function() {
  $scope.traumaSearch = !$scope.traumaSearch;
  if ($scope.traumaSearch) {
    $scope.traumaText = "Select a location...";
    canvas.style.cursor = 'crosshair';
  } else {
    $scope.traumaText = "Trauma Center";
    canvas.style.cursor = '';
  }
}

/* Police Search functionality */
$scope.policeSearch = false;
$scope.policeText = "Police Station";
$scope.togglePoliceSearch = function() {
  $scope.policeSearch = !$scope.policeSearch;
  if ($scope.policeSearch) {
    $scope.policeText = "Select a location...";
    canvas.style.cursor = 'crosshair';
  } else {
    $scope.policeText = "Police Station";
    canvas.style.cursor = '';
  }
}

/* Fire Search functionality */
$scope.fireSearch = false;
$scope.fireText = "Fire Station";
$scope.toggleFireSearch = function() {
  $scope.fireSearch = !$scope.fireSearch;
  if ($scope.fireSearch) {
    $scope.fireText = "Select a location...";
    canvas.style.cursor = 'crosshair';
  } else {
    $scope.fireText = "Fire Station";
    canvas.style.cursor = '';
  }
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div id="marker">
  <!-- LOCATION BUTTONS -->
  <h2>Find the Nearest Emergency Services</h2>
  <button class="btn btn-info" ng-click="toggleTraumaSearch()">{{traumaText}} </button>
  <button class="btn btn-info" ng-click="togglePoliceSearch()">{{policeText}}</button>
  <button class="btn btn-info" ng-click="toggleFireSearch()">{{fireText}}</button>
</div>

I attempted to address this issue by using ng-disabled (referenced in disable the button after one click using ng-disable) but encountered difficulties. The second solution, involving an additional "if" statement, caused the function to malfunction, possibly due to incorrect syntax on my part.

Another approach I tried was restricting user input within the function's scope. It is possible that my attempt to achieve this with "$scope.toggleFireSearch.input = false" was misguided due to syntax errors.

If you have any suggestions for directives that could effectively solve my dilemma, I would greatly appreciate your advice.

Answer №1

To validate whether a variable in your scope is true, simply utilize the ng-if="VARNAME" element tag. Consider using distinct variables for each search element or an array. With this setup, you can create a function like ToggleWhateverSearch() to set one variable to true while setting all others to 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

Utilize a different variable for the display without altering the original model

Within my object, there resides a collection of items labeled X. Each X contains a list of Y items, and each Y contains a list of Z items. I am seeking a solution to hide the span upon clicking a button without altering the object itself. One approach inv ...

Having difficulty transferring data from a JSON file on a different domain to a variable using Ajax

As a beginner in Ajax, I am currently exploring the use of Ajax Cross domain functionality to fetch data. The Ajax function is triggered from test.php, which then calls stats.php to request the desired data. The content of Stats.php: <?php $data = ...

Serverless Functions in ZEIT Now - Customizing Routes with Parameters

I successfully implemented 4 serverless routes /api/list (GET) /api/add (POST) /api/update/:id (PUT) /api/remove/:id (DELETE) These routes were added to the api/now.json file in the following format: {"src": "/api/list", "dest": "./list.js", "methods": ...

Unable to transfer data from factory to controller

I am encountering an issue with my code that involves a factory: angular.module("mainApp", ["kendo.directives"]) .factory("getFormats", function() { var searchStr="list&John&1&1".substr(1); //I can see the searchStr here ...

When trying to use TypeScript with next.js, encountering an unexpected token `?` error is not

Having an issue with next.js, the command npm run dev keeps failing due to a syntax error related to an optional property in a tsx file: Syntax error: Unexpected token 44 | 45 | type State<T_HT> = { > 46 | ghostHighlight: ?{ | ...

Ways to store AJAX response data for future use

I am struggling with implementing the getState function. My goal is to update a field on click using a state value retrieved from an AJAX call. I have come across mentions of promises in other responses, but I am unsure how to integrate them into my code ...

mention the element to display on the pagination

I find the logic here quite puzzling. This particular code block seems to be related to pagination, as it involves a function that is triggered upon clicking. componentDidUpdate() { const { location } = this.context; const { query } = this; if ...

Unexpected Behavior: using setTimeout in a ReactJS class component leads to incorrect value retrieval

App Component: class App extends React.Component { constructor() { super(); this.state = { user: 'Dan', }; } render() { return ( <React.Fragment> ...

Adjust size of element in relation to Background Cover using jQuery

Can you help me solve a challenge I'm facing? I have a website with a fullsize background image, and I need to attach a div to a specific position on the image while ensuring that it scales in the same way as the background image with the "background ...

Tips for increasing the number of inputs within a form using <script> elements

I am currently working on a form within the script tags and I would like to include additional input fields before submitting. However, the submit button seems to be malfunctioning and I suspect that there may be an issue with how I am accessing it in my c ...

Repair the navigation bar once it reaches the top of the screen using ReactJS

I have a webpage that contains specific content followed by a bar with tabs. My goal is to have this bar stay fixed at the top of the screen once it reaches that position while scrolling down, and only allow the content below the fixed bar to continue scro ...

Counting the visible elements in Slick carousel

I have implemented slick.js to display a grid of 6 items (2 rows, 3 columns) per slide. I am looking for a way to include both the standard prev and next arrow navigation as well as an indication of the active item count for pagination assistance. For exa ...

update the element that acts as the divider in a web address (Angular)

Is it possible to modify the separator character used when obtaining the URL parameters with route.queryParams.subscribe? Currently, Object.keys(params) separates the parameters using "&" as the separator. Is there a way to change this to use a differe ...

The step-by-step guide to testing an Angular promise using Jasmine

An issue arises when attempting to test the code below using Jasmine, as the console.log in `then` is never called. The question remains: is this problem related to Angular or Jasmine? describe("angular test", function() { var $q; ...

Unchecked AngularJS checkbox when a different option is selected

<select ng-model="choose"> <option value="1"></option> <option value="2"></option> <option value="3"></option> </select> If the selected value is 1 or 3, a checkbox should appear. <input type ...

Modifying input type using Angular

After reading various posts discussing the use of ng-switch or directives for changing input types, I have created this code. However, it seems to be quite redundant. <input ng-change="change();" ng-if="! item.data && item.mapping == 'pass ...

Sorting data in AngularJS using the OrderBy filter in ascending

I am using ng-repeat in my code: <label ng-repeat="atp in Keywords | unique:'atp'"> {{atp}} </label> The values in atp are as follows: client animal zoo boat I want the final output to be: animal boat client zoo Thank you for ...

Implementing AJAX in Rails for the new/create action can greatly enhance the user

I have a timeline with event sections on it that allow you to create, view, update, and delete events directly on the timeline page. Currently, the functionality for deleting an event is working smoothly as the delete section refreshes along with the timel ...

Find the flowplayer when the page loads

Is there a way to make a flowplayer seek at the page load without it resetting? I've tried this: $(document).ready(function() { $f(0).seek(5); }); also tried: $(document).ready(function() { while(!$f(0).isLoaded()) { $f(0).seek(5); } }); ...

Function is not triggered in React component

When the LoginPage calls AuthForm, the structure is as follows: function mapDispatchToProps(dispatch: Redux.Dispatch<any>) { return { signUpWithEmail: function(email: string, password: string) { // bla bla }, }; } handleForm ...