A guide on clearing the selected value in a dropdown menu with Angular.js

Can someone assist me with setting the drop-down value to blank after completing an action in Angular.js? Below is my code explanation:

<div style="height:270px; overflow-x:hidden; overflow-y:scroll;" ng-show="viewOrderTable">
<div class="table-responsive dashboard-demo-table">
<select class="form-control"  id="status" ng-model="order_status" ng-change="changeOrderStatus(order_id,shipping_email,p.pro_data_id,order_status,p.days)">
 <option value="">Select Status</option>
                                                        <option value="In-progress">IN-PROGRESS</option>
 <option value="Dispatch">DISPATCH</option>
 <option value="Delivered">DELIVERED</option>
<option value="canceled" ng-if="p.pro_status =='Ordered' && p.pro_status=='In-progress'">CANCELED</option>
 <option value="Returned" ng-if="p.days <= 48 && p.pro_status=='Delivered'">RETURN</option>
 </select>
</div>
</div>

When the user selects DISPATCH from the list above, the following form appears.

<div class="" ng-show="viewDispatch">
<div style="padding-bottom:20px;">
<div class="col-md-6">
                            <div class="input-group bmargindiv1 col-md-12">
                            <span class="input-group-addon ndrftextwidth" style="width:120px; text-align:left;">Dispatched Date& Time :</span>
                            <div class="datepicker" date-format="dd-MM-y h:mm:ss" button-prev='<i class="fa fa-arrow-circle-left"></i>' button-next='<i class="fa fa-arrow-circle-right"></i>'> 
                                <input type="text" name="dispatch_date" class="form-control" id="dispatch_date" ng-model="dispatch_date" placeholder="Add Dispatched Date& Time" ng-change="clearField('dispatch_date');" />
                                </div>
                            </div>
                        </div>
                        <div class="clear"></div>
                    </div>
                    <div class="col-md-12 text-right">
                        <button type="button" class="btn btn-success" ng-click="addDispatchData();">Submit</button> <button type="button" class="btn btn-danger" ng-click="clearDispatchData();">Back</button>
                    </div>
</div>
</div>

The controller-side code for this action is provided below.

$scope.changeOrderStatus=function(orderid,email,prodataid,order_status,hour){
    if(order_status=='Dispatch'){
                $scope.viewOrderTable=false;
                $scope.viewDispatch=true;
                pro_dataId=prodataid;
                order_Id=orderid;
                dStatus=order_status;
                email=email;
            }
}

After clicking the back button, it returns to its original state but the dropdown option DISPATCH remains selected when I want it to reset. Here is the clearDispatchData function:

$scope.clearDispatchData=function(){
        $scope.viewOrderTable=true;
        $scope.viewDispatch=false;
        $scope.order_status='';
    }

I have tried the above solution but it's not resetting. Please help me resolve this issue.

Answer №1

If you want to initialize a value in your html file, you can utilize ng-init.

<select class="form-control"  id="status" ng-model="order_status" ng-change="changeOrderStatus(order_id,shipping_email,p.pro_data_id,order_status,p.days)" ng-init='order_status == 0'>

Answer №2

If you are experiencing digestion issues, one solution to consider is utilizing the $timeout function in your code. Make sure to inject $timeout into your controller before implementing it.

$scope.clearDispatchData=function(){
     $timeout(function() {
          $scope.viewOrderTable=true;
          $scope.viewDispatch=false;
          $scope.order_status='';
     });  
}

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

Tips for halting the live graph plotting based on a specific condition with canvas js

I have a piece of code that generates a live graph based on probabilities. I am looking for a way to stop the plotting process as soon as a specific condition is met. <script> window.onload = function () { var dps = []; // dataPoints var c ...

Showing Excel Data Frame on a webpage with tabular arrangement

I'm in the process of creating a web-based Dashboard using Python for the backend and AngularJS for the frontend. As a beginner, my initial task involves displaying Excel data on the webpage in a tabular format. Below is the Python code I've wri ...

Optimal approach for creating and deploying Docker images

After successfully setting up the initial pipeline for my Angular application, which operates within a Node image in Docker, I followed this process: first, push to Gitlab then initiate a Hook to trigger the Jenkins Build. This is followed by executing a D ...

Problem encountered in AngularJS CheckBox binding where the checkbox remains unchecked despite the model having a checked value

I am working with AngularJS and have implemented the following code: <div > <input type="checkbox" ng-model="vehicleDetails.selfOwned"> Owned </div> <div> {{vehicleDetails.selfOwned}} </div> The mo ...

Determine in Node.js if a word has been altered or has an additional letter compared to the previous word

I'm currently developing a feature for my Discord bot that allows users to play a word game. The objective is to input words that either change one letter from the previous word or add a letter. To implement this, I am utilizing the following function ...

What is the best way to rotate an image using AngularJS?

Hey there, I've got an image that I need help rotating. There are two buttons - left and right - that should rotate the image 45 degrees in opposite directions. I attempted to create a directive using the jquery rotate library, but it's not worki ...

Guide to reference points, current one is constantly nonexistent

As I work on hosting multiple dynamic pages, each with its own function to call at a specific time, I encounter an issue where the current ref is always null. This poses a challenge when trying to access the reference for each page. export default class Qu ...

Initial position of jQuery slider

A while back, I came across some slider code on a website and copied it. Unfortunately, I can't seem to locate the source now. Here is the code snippet: slides.min.jquery.js $(function(){ $('#slides').slides({ preload: true, ...

Creating randomized sequences using JavaScript

One of my hobbies involves organizing an online ice hockey game league where teams from different conferences compete. It's important to me that every team gets an equal number of home and away matches throughout the season. To simplify this task, I&a ...

Compiling modal window content in AngularJS can lead to the creation of controllers that are left disconnected

I implemented a modal window triggered by fancybox in my project. Once the modal is displayed, fancybox triggers a "modalShown" event that is listened for by AppController. In this listener, $compile is called on the modal content to create the ModalContro ...

Attempting to replicate the action of pressing a button using Greasemonkey

I am currently working on a greasemonkey script to automate inventory updates for a group of items directly in the browser. I have successfully implemented autofill for the necessary forms, but I am facing challenges with simulating a click on the submissi ...

Send a redirect after a certain delay to a URL stored in a variable outside of the current scope

Upon making an ajax request, the JSON response contains a link that needs to redirect the user to another page after 3 seconds. The current approach used is: response = JSON.parse(res); var link = response.link; setTimeout("window.location.href=link",300 ...

AngularJS version 1 not enabling 2-way binding within setTimeout function

I am trying to implement a feature where the submit button is disabled during the saving process and then enabled once the save is complete. However, I am facing an issue where even though the flag changes, the view does not update as expected with two-wa ...

Having trouble with less.modifyVars not functioning properly in Visual Studio?

I attempted to dynamically change the LESS variable using HTML within an AngularJS function. The code worked fine on XAMPP with simple HTML and CSS, but when I transferred it to an enterprise app in Visual Studio, it stopped functioning properly. While the ...

JavaScript's Automated Retail Machine Feature

I'm still in the early stages of learning Javascript, having dabbled with it for a couple of weeks. This marks my first time posting, so I apologize if my explanation is not comprehensive enough, but I'll do my best! My goal is to create a funct ...

leveraging jQuery across an Angular 2 application as a global tool

I'm currently exploring ways to incorporate jQuery globally throughout my angular 2 application. The only comprehensive resource I've come across so far is this Stack Overflow answer. However, despite my efforts, I haven't been able to make ...

Adjust the dimensions of the thead, tbody, and td elements on the fly

I've implemented this property in a .css file on the table shown below and there are 9 records. .fixed tbody td, thead th { width: 5.2%; float: left; } In my scenario, when there are 4 columns, the width should be 23.2%; for 5 columns ...

Using jQuery setTimeout within a forEach loop

Currently, I am fetching an array of messages using 'getJSON method. My intention is to display each message for 3 seconds before moving on to the next one. The process involves loading an HTML file and applying a CSS class to each message. However, m ...

Changing a global variable via an AJAX call

I seem to be facing a common issue that many others have encountered. Despite my understanding that global variables can be modified inside functions in Javascript, I am struggling with this concept in practice. var lastMessage = 0; function loadChat() { ...

Submitting a form with JQuery and Ajax technology

I am encountering an issue where I need to submit a form within Ajax and I keep getting a 'form.submit is not a function' error in jQuery. $("#form").validate({ // Define validation rules rules: { type: "required", group ...