Using AngularJS to attach an event listener

I am faced with the challenge of detecting when a variable changes on the HTML side, but I am struggling to implement an event listener ($watch) in my Angular controller.

Here is the specific issue that I am attempting to resolve:

 <div class="form-group">
                    <label class="col-md-3 control-label">Please Select the Device</label>
                    <div class="col-md-9">
                        <select class="form-control" ng-model="reportCtrl.selectedDevice" ng-options="item.name as item.MyName for item in reportCtrl.deviceList">
                        </select>
                    </div>
                </div>

                <div class="form-group" ng-if="reportCtrl.selectedDevice">
                    <label class="col-md-3 control-label">Please Select the Sensor</label>
                    <div class="col-md-9">
                        <select class="form-control">
                        </select>
                    </div>
                </div>

The options in the dropdown are populated based on the device list. When a user selects a device, I need to trigger another call to retrieve sensor properties within that device. My goal is to initiate this call when a user selects a device.

How can I incorporate an event listener in this scenario to execute the necessary call?

Answer №1

To initiate the call, you can utilize ng-change as the trigger mechanism. Once the response is received, the options of your second select will update automatically (make sure to include the ng-options attribute).

<select class="form-control" ng-change="updateSelection()" ng-model="ctrl.selectedItem" ng-options="item.value as item.label for item in ctrl.itemList">

Another option is to employ $watch, although it may not be as efficient as ng-change in this specific scenario.

$scope.$watch('ctrl.selectedItem', function(newValue, oldValue){...});

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

Is there a way to find all records created at a particular time daily through a query?

I understand how to search for documents within a particular range, but I am unsure of the query needed to retrieve all documents in a collection that were created at 3PM. Assuming there is a field called createdAt where this information is stored as Jav ...

Add the item to an array to store its state

I have a state variable that is initially set as an empty array const [boxes, setBoxes] = useState([]); const [showAddGalley,setShowAddGalley]=useState({galleyNo:""}); I created a function to handle form submissions, where I want to update the b ...

Ways to merge several getServerSideProps functions

Within my project, I have two important pages: index.js and other.js. In index.js, there exists a crucial method known as getServerSideProps: export async function getServerSideProps(context) { //code here } The challenge arises when I realize that I ...

Troubleshooting problem with Snapjs Drawer and navigating through scrolling content

Currently, I am utilizing Snap JS to implement a drawer on the left with content displayed on the right. Despite this setup, I am encountering an issue where scrolling is still enabled for the content on the left when the drawer is open. I am seeking guid ...

Using Javascript to read a JSON string displayed in a URL

I'm working on developing a straightforward web application that extracts latitude and longitude data stored in a JSON format and places markers on a Google map based on this information. Currently, there is a program running on a server that generate ...

How can I align Javascript output vertically in the middle?

I am currently facing an issue with a JavaScript clock displaying in a narrow frame: <!DOCTYPE html> <HTML> <HEAD> <TITLE>Untitled</TITLE> <SCRIPT> function checkTime(i) { if (i < 10) { ...

Enhance user experience by customizing the visibility of swatches for custom buttons on the KendoUI for Angular

Is there a way to add custom buttons to a Kendo Grid toolbar and toggle their visibility by simply clicking on another button? For example, clicking Button1 would hide Button1 and show Button2, and vice versa. I have successfully added these buttons to th ...

The file_get_contents() function encountered an issue as the content type was not specified, leading to it assuming

I am currently working on a project using PHP and JavaScript. I need to call an API from the camera using PHP code. I am using the file_get_contents function and Post request for this purpose. Below is the code snippet: $value = $_POST['TakeNewPic&ap ...

Sorting and Uploading Images made Easy with Drag-and-Drop Functionality

Currently, I am developing a webpage and exploring the option of implementing a drag-and-drop image upload system that allows users to remove and sort images before uploading. I am seeking your opinion on this matter. I am concerned about potential issue ...

Tips on how to postpone the loading of an image when utilizing a CSS animation on a different element

I'm currently working on a webpage where I have integrated CSS animations to move images around. When you click on these images, a larger version of the image along with a related paragraph is supposed to appear in its place. Initially, I had set up a ...

What are the various jQuery methods that can be utilized in the object parameter of a jQuery element creation request?

According to a post by John Resig on his website at http://ejohn.org/apps/workshop/adv-talk/#3, it is mentioned that methods can be attached using the object parameter. While 'text' appears to function correctly, any other content in the object ...

Disable Three.js when WebGL is not supported: Tips and tricks

I read about how to switch to canvasrenderer if webgl is not supported. renderer = Detector.webgl? new THREE.WebGLRenderer(): new THREE.CanvasRenderer(); I'm not sure how to completely remove everything if webgl is not detected. I have an image in t ...

Retrieve the chosen option from a dropdown menu and transfer it to the submit button in PHP

I have a unique situation where I need to extract and store selected values from two separate drop-down menus generated from arrays. The goal is to pass these selected values in the submit button link. For example, if a user selects "123" for email and "t ...

What is the proper way to retrieve the app object from a module in ExpressJS?

In my application, I am running an Express app by setting it up in the app.js file: var app = express(); Once the app is created, I can define variables like this: app.set('host', 'myhost') Now, within my project, I also have a sepa ...

Integrating jquery events and functions with angularjs

Recently, I've come across this piece of code: $('.btn').click(function() { $(this).removeClass('big-red-button'); $(this).addClass('btn-loading'); $(this).find('.text').text('Working..'); ...

Is it possible to disable the next and previous buttons in jCarousel?

I am currently using the jCarousel slider from for my website. The slider displays one image at a time, with a total of four images. I want to hide the previous arrow when displaying the first image and hide the next arrow when displaying the fourth image ...

Error: Node Command Line Tool cannot be located using NVM bash

Creating a basic node CLI tool has proven to be challenging when using NVM. Despite installing node locally and attempting to switch to system with nvm use system, the CLI file remains inaccessible. The code snippet includes the following shell designation ...

anchor text substitution for hyperlink

I currently have a code snippet that replaces a span class with a hyperlink. The hyperlink contains an abbreviation, and the alternate text for the link also includes the same abbreviation. Now, I am looking to replace the second abbreviation in the alte ...

How to integrate toastr into your AngularJS application

I have recently attempted to incorporate toastr into my AngularJS application: app.module("MyApp", ['toastr']); Unfortunately, I am experiencing difficulties as the toastr module cannot be located. I was under the impression that toastr was an ...

Update the div content to completely replace it with fresh data using Ajax

I'm currently working on implementing a currency switcher feature for my website, allowing users to toggle between two different currencies. The site is a reservation platform with a booking form displayed on the left side and the booking details occu ...