Prevent the chosen item from being transferred from drop down One to drop down Two using Angular 2 Material

<div class="row no-overflow">
    <div class="col-md-1 window-pad-height">
        <mat-label> Opportunity 1: </mat-label>
    </div>
    <div class="col-md-2 no-overflow">
        <mat-form-field class="no-overflow">
            <mat-select placeholder="select" [(ngModel)]="oppurtunityOne" [disabled]="selectedRankingCriteria.viewValue!=='Custom'">
                <mat-option *ngFor="let op1 of OppurtunityList" [value]="op1">
                    {{op1.viewValue}}
                </mat-option>
            </mat-select>
        </mat-form-field>
    </div>
</div>

<div class="row no-overflow">
    <div class="col-md-1 window-pad-height">
        <mat-label> Opportunity 2: </mat-label>
    </div>
    <div class="col-md-2 no-overflow">
        <mat-form-field class="no-overflow">
            <mat-select placeholder="select" [(ngModel)]="oppurtinityTwo" [disabled]="selectedRankingCriteria.viewValue!=='Custom'">
                <mat-option *ngFor="let op of OppurtunityList" [value]="op" [disabled]="oppurtunityOne.viewValue">

                    {{op.viewValue}}
                </mat-option>
            </mat-select>
        </mat-form-field>
    </div>
</div>


<div class="row no-overflow">
    <div class="col-md-1 window-pad-height">
        <mat-label> Opportunity 3: </mat-label>
    </div>
    <div class="col-md-2 no-overflow">
        <mat-form-field class="no-overflow">
            <mat-select placeholder="select" [disabled]="selectedRankingCriteria.viewValue!=='Custom'">
                <mat-option *ngFor="let op3 of OppurtunityList" [value]="op3">
                    {{op3.viewValue}}
                </mat-option>
            </mat-select>
        </mat-form-field>
    </div>
</div>


<div class="row no-overflow">
    <div class="col-md-1 window-pad-height">
        <mat-label> Opportunity 4: </mat-label>
    </div>
    <div class="col-md-2 no-overflow">
        <mat-form-field class="no-overflow">
            <mat-select placeholder="select" [disabled]="selectedRankingCriteria.viewValue!=='Custom'">
                <mat-option *ngFor="let op4 of OppurtunityList" [value]="op4">
                    {{op4.viewValue}}
                </mat-option>
            </mat-select>
        </mat-form-field>
        <button mat-raised-button color="primary">View</button>
    </div>
</div>

JSON DATA

OppourtunitiessList: any[] = [
    { value: 'col-1', viewValue: 'datatoDisplay1' },
    { value: 'col-2', viewValue: 'datatoDisplay2' },
    { value: 'col-3', viewValue: 'datatoDisplay3' },
    { value: 'col-4', viewValue: 'datatoDisplay4' },
    { value: 'col-5', viewValue: 'datatoDisplay5' },
    { value: 'col-6', viewValue: 'datatoDisplay6' },
    { value: 'col-7', viewValue: 'datatoDisplay7' },
    { value: 'col-8', viewValue: 'datatoDisplay8' },
    { value: 'col-9', viewValue: 'datatoDisplay9' }
  ];

How to disable a selected option from the dropdown using TypeScript?
What logic should I implement here to disable the selected option?
Is it possible to handle this using ngModel?

Answer №1

Implement a feature that allows for dynamic evaluation within your component:

<!-- HTML -->
<mat-select placeholder="Choose Option" 
[disabled]="disableSelectOption(selectedCriteria.value)">

// TypeScript
disableSelectOption(option){
    if (option === 'specificValue'){
        return true; // disable option
    } else {
        return false; // enable option
    }
}

Answer №2

Here is a modified version of your code snippet for the second dropdown menu:

<div class="col-md-2 no-overflow">
    <mat-form-field class="no-overflow">
        <mat-select placeholder="Choose Option" 
                    [(ngModel)]="optionTwo" 
                    [disabled]="selectedCriteria.value !== 'Custom'">
            <mat-option *ngFor="let option of OptionsList" 
                        [value]="option" 
                        [disabled]="optionOne.value === option.value">
                {{option.label}}
            </mat-option>
        </mat-select>
    </mat-form-field>
</div>

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

The type 'Promise<UserCredential>' cannot be assigned to

import React, {createContext, useContext, useEffect, useState, ReactNode} from "react"; import { auth } from '../utils/init-firebase'; import { createUserWithEmailAndPassword } from "firebase/auth" type ButtonProps = { ch ...

Creating TypeScript types for enums can make your code more robust and ensure that you are using

I need to create an interface based on the values of an enum for a React use-case. The enum contains key value pairs that are used as form IDs. When the value of an input element is changed in an event listener, I want to set the state using this.setState( ...

Updates made in the browser are not being reflected in the Jhipster 4 generated app

I have attempted the following solutions: Modifying webpack.dev.js by setting reload : true new BrowserSyncPlugin({ host: 'localhost', port: 9000, proxy: { target: 'http://localhost:906 ...

RxJS isn't able to transfer data from one Observable to another Observable

Recently diving into the world of RxJS, I've hit a roadblock while trying to follow a course. My current challenge is feeding a list of lessons to an *ngFor using the async pipe. To address this, I initialized: allLessons$: Observable<Lesson[]&g ...

A frequently used button found in the parent component of an Angular application that can be accessed and utilized by its

The configuration for the Angular application includes the following routes: componentRoutes: Routes = [ {path: 'child', canActivate: [guardService], component: ParentComponent, children: [ {path: '', component: ...

What is the most effective method for obtaining a position in Angular using async await?

I have a code that works perfectly fine, but it's not very aesthetically pleasing. My goal is to refactor it into a service. However, when I attempt to use async await functions, it doesn't work as expected: Take a look at this: ngOnInit() { ...

Building stateless functional components in React using Typescript version 0.14

Demonstration: import * as React from 'react' declare function obtainMarineLife(x: any): any; declare var Tank: any; var OceanicHabitat = ({category}) => ( <Tank> {obtainMarineLife(category)} </Tank> ); let y = <Ocea ...

Guide to setting up identically named properties in Child container components

As I am still fairly new to React and its concepts, I may not be executing this correctly. Although the question might seem lengthy, I'll try my best to keep it simple. I have created a component for TableHead that extends some material-ui components ...

The error message "TypeError: Unable to access the 'getFullWidth' property of an undefined value when using TSLint and TypeScript" was

I have been using Dan Wahlin's tutorials and online examples to set up Gulp and Typescript, but I am facing an issue with the tslint() function. The problem occurs in my code as follows: node_modules\tslint\lib\language\walker&bso ...

Global variable's value cannot be accessed outside the function in which it was inserted

I am facing an issue where I cannot access the value of var Login inside submit() function. This could possibly be due to a race condition. What could be causing this blockage in accessing the value of var Login? Here is the code snippet: class ABC { ...

Prevent data loss on webpage refresh by using Angular's local storage feature

As a beginner in Angular, I am exploring ways to retain user input and interactions on my webpage even after a refresh. After some research, I came across using local storage as a viable solution. A different answer suggested utilizing the following code s ...

The utilization of ~ (tilde) for SCSS file imports seems to be malfunctioning in Angular 6

Having some issues with importing SCSS files in Angular6, need clarification on a couple of things: If I import my partials once in the global src/sass/styles.scss, do I still need to import them individually in all component.scss files? Is there a way t ...

Tips for efficiently handling input and output parameters in a dynamically generated component in Angular2 RC4

I need to pass several parameters to a component: <sentence [sentence]="text.title" [selectedWord]="selectedWord" (selectionChanged)="selectionChanged($event)"></sentence> To dynamically create a component, I am using the following code: ad ...

Exploring the potential of React with Typescript: Learn how to maximize

Having some difficulties working with Amplitude in a React and Typescript environment. Anyone else experiencing this? What is the proper way to import Amplitude and initialize it correctly? When attempting to use import amp from 'amplitude-js'; ...

Exploring the power of Angular Module Federation in leveraging remote services

I am breaking down an Angular application into multiple microfrontends. One of these microfrontends manages the shopping cart, and it includes a service for storing added products. The main shell microfrontend needs to access this service to display the nu ...

The type does not contain a property named 'x' - Error in Promise syntax - TS2339

I encountered a problem while working with Typescript that I couldn't quite figure out. Although I came across similar issues in other topics, I'm still struggling to find a solution for my particular issue. I can easily log userCredential.user.m ...

Is there a way to ensure that fields in a sub component are validated whenever we attempt to switch the Tab using a route

Hi there, I could really use your assistance. I've done some research, but I haven't been able to find a suitable solution for my problem. I have this shared component that contains the following code which enables tab navigation through various ...

Customizing the Global Error Handler in Angular

Our project utilizes a global error handler to manage errors. However, we have encountered a unique scenario where we need to handle a specific case separately but are unable to do so. The global error handler is configured in the app.module.ts: { provide: ...

Implementing nested functions in Angular2 with TypeScript

Currently in the process of migrating from Angular 1 to 2, encountering a situation with nested functions in JavaScript: function normalizeDoc(doc, id) { function normalize(doc){... After removing the "function" keyword, TypeScript errors have starte ...

Issues arise when using Android BluetoothLeAdvertiser in Nativescript applications

I've been working on creating a Nativescript application that can send Bluetooth low energy advertisements. Since there are no existing Nativescript plugins for this functionality, I decided to develop a Java library (with plans to add a Swift library ...