Tips for Loading a Selected Option from an Ajax Call in Angular JS on Page Load

How do I automatically set the selected option from an ajax call when the page loads? I am fetching zones using an ajax call and I want to trigger the change function of the zones on page load in order to set the selected option of the cities select box. Here is my code snippet:

 <select class="form-control" name="zone_id" ng-model="zone_id" name="zone_id" ng-change="getSearchCities(zone_id)">
    <option>Choose zone</option>
    <option value="20" selected>Zone 1</option>
    .....
  </select>

  <select class="form-control" name="city_id">
    <option>choose city</option>
    <option value="{{ city.city_id }}" ng-repeat="city in cities">
    {{ city.name }}
    </option
  </select>


  <script>
    var SApp = angular.module('searchApp', []);
    SApp.controller('searchCtrl', function($scope, $http) {
      $scope.getSearchCities = function(zone_id) {
        // alert(zone_id);
        $http.get("zones.php?zone_id="+zone_id).then(function(response) {
          $scope.cities = response.data.city;
        });
      }
    }); 
  </script>

Answer №1

After some thorough investigation, I have discovered the solution: 1. Implement ng-options instead of using ng-repeats on the option tag 2. Invoke the getSearchCities function as follows: $scope.getSearchCities($scope.zone_id);

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

Exploring Unanchored Substring Queries in Loopback API

Searching for a solution to implement a basic substring query using the loopback API for a typeahead field. Despite my efforts, I have not been able to find a clear answer. I simply want to input a substring and retrieve all brands that contain that subst ...

I must design a unique layout for my website

I am creating a webpage with various shapes and elements. However, I am facing a challenge with creating a shape that is commonly used. In platforms like Khan Academy, there is simulated programming where shapes can be easily created with a simple command ...

Leveraging Object.assign for updating fields in Firebase documents

Currently, I am working on a website that allows users to create new projects by filling out a form with all the necessary project information. Within this form, there is a file input field where users can upload images and documents. I have successfully i ...

In the tutorial for creating a basic web application, an issue arises with AWS Lambda stating that it "cannot locate module aws-sdk"

As I embark on my AWS journey, I decided to start from scratch by following the Build a Basic Web Application tutorial. Everything seemed straightforward until I encountered an unexpected error while trying to include the basic aws-sdk module! In Brief: ...

Issues with React Material UI Dialog Displaying Incorrect Information

As I experiment with React Material UI dialog boxes, I've encountered an issue that has me puzzled. I have an object 'a', and when I click on a button in a list, it should display the respective ID number. However, instead of showing the cor ...

What is the process for changing proxy settings through the command line when using Create React App?

I recently created a React project using Create React App and set up the development server to proxy API requests through the proxy setting in my package.json: ... "proxy": "https://dev-backend.example.com" ... However, I am looking ...

Misplace reference to object during method execution

Here's a simple demonstration of the issue I'm facing. The count function is supposed to keep track of the number of items returned from a query. However, my current implementation causes me to lose reference to the function when calling it from ...

Tips for utilizing the "Sign In with Apple" feature through Apple JS

For implementing "Sign In with Apple" on a web platform using Apple JS, you can refer to the code example available at this link. Now, the query arises: where can I find the Client ID required for this process? I have tried using the app id identifier fro ...

Converting HTML/Javascript codes for Android Application use in Eclipse: A Step-by-Step Guide

How can I implement this in Java? Here is the HTML: <head> <title>Google Maps JavaScript API v3 Example: Geocoding Simple</title> <link href="http://code.google.com/apis/maps/documentation/javascript/examples/default.css" rel="styles ...

Using AngularJS ng-repeat within a modal pop-up

Can someone help me with an issue I'm having? I want to display the details of a single person in a modal when a button is clicked on the table. However, my code currently displays the details of everyone. Here's what I have: <div id="myModal ...

Extra Pathway

Having trouble adding a second route to the /privacy page. The error "Cannot GET /privacy.html" keeps popping up. Any suggestions? This code seems to be failing to display the privacy.html content app.get('/privacy', function(req, res) { res.s ...

Retrieving data from a file results in receiving blank strings

Struggling to access the data within a directory of files, I've encountered an issue where the data doesn't seem to be read correctly or at all. Even though there is content when opening the files individually, when attempting to examine their co ...

Only render the div content in jQuery when the user has made a selection

Looking to optimize my website by incorporating tabs with a large amount of HTML content without slowing down the page load. Is there a way to use jQuery to load the div content only when each tab is selected? The basic structure of the HTML code would be ...

How do you trim a string and display the final 3 characters?

When dealing with a list of objects, I want to ensure that the chain of tasks does not become too long and break the table or appear aesthetically unpleasing. Therefore, my goal is to trim the tasks and display only the last 3. In the image below, multiple ...

Build a Node.js application with Express to host static files

I am attempting to provide my static files "web.html" and "mobile.html", but I want them to be served only if the user is accessing from a web or mobile device. After some research, I came up with this code: var express = require('express'); va ...

Access the value of a JavaScript global variable using Selenium in Python

I need to access a value from a global variable in JavaScript: clearInterval(countdownTimerTHx); var saniye_thx = 298 // <--- Variable here function secondPassedTHx() { https://i.sstatic.net/odIbh.png My goal is to retrieve the value " ...

Learn the Method Used by Digg to Eliminate "&x=0&y=0" from their Search Results URL

Instead of using a regular submit button, I have implemented an image as the submit button for my search form: <input id="search" type="image" alt="Search" src="/images/searchButton.png" name="" /> However, I encountered an issue in Chrome and Fire ...

Swapping out data points using JQuery

What could be causing line 10 to return null? Click here for the code file The code seems to function properly with line 40, but not with line 10. ...

retrieve the element from a different array using AngularJS version 1

How can I fetch a value from another array? I am using Ionic with Angular 1. In Angular 1, I have created two arrays as shown below: $scope.customers = [{id:1111, name:"Milan Shop", city:"Milan"},{id:2222, name:"Roma Shop", city:"Roma"}] $scope.SizeOfCu ...

Display the variance in values present in an array using Node.js

I am facing a challenge and need help with printing arrays that contain both same and different values. I want to compare two arrays and print the values that are present in both arrays in one array, while also creating another array for the differing val ...