AngularJS doesn't display data in a separate file when using ng-select

I am currently utilizing AngularJS to develop a web application. If you would like to view the complete code, you can access it through this link (sample with b.0001.html). AngularJS example

My question is how can I retrieve the value ($scope.confirmed in $scope.change function) in the controller when a user selects data from a form? The issue arises when I try to work with separate files rather than everything being contained within one file, especially when including another file with a form using routing in AngularJS.

The form that I'm including in the main file utilizes the routing mechanism as shown below:

<select ng-model="confirmed" ng-change="change()" id="ng-change-example1">
<option>11</option>
<option>12</option>
<option>13</option>
<option>14</option>

<input type="checkbox" ng-model="confirmed" id="ng-change-example2"/>
<label for="ng-change-example2">Confirmed</label><br/>
<tt>debug = {{confirmed}}</tt><br/>
<tt>counter = {{counter}}</tt><br/>

<tt>onchange_var_url = {{myurl}}</tt><br/>

<ul>
  <li ng-repeat="x in names">
    {{ x.Name + ', ' + x.Country }}
  </li>
</ul>

The myurl variable should capture the value from the user's selected option, but it's not working as expected. Below is the controller for reference:

angular.module('changeExample', [])
.controller('ExampleController', ['$scope','$http', function($scope,$http) {

$scope.counter = 0;

$scope.myurl = 'result from confirmed on change on load: ' + $scope.confirmed;

$scope.change = function() {
    $scope.counter++;

    $scope.myurl = 'result from confirmed on change: ' + $scope.confirmed;

    $http.get("angularjs-data/json-data-0001.json?var=" + $scope.confirmed)
        .success(function (response) {
            $scope.names = response.records;
        });
};

}]);

Answer №1

Just a quick note regarding your <option> tags - make sure to include some values

var xyz = angular.module('xyz', []);

xyz.controller('xyzCtrl', function($scope) {
  $scope.counter = 0;
  $scope.selectedValue = 10;

  $scope.update = function() {
    ++$scope.counter;
    $scope.check = $scope.selectedValue;
  }
});
  
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="xyz" ng-controller="xyzCtrl">
  -{{ var }}-
  
  <select ng-change="update()" ng-model="selectedValue">
    <option value="10">10</option>
    <option value="11">11</option>
    <option value="12">12</option>
    <option value="13">13</option>
  </select>
  <pre>
  counter: {{ counter }}
  selectedValue: {{ selectedValue }}
  </pre>
</div>

Answer №2

I believe I have the solution to your query. The key is to assign the selected value in the change function. What you need to do is insert the selected value in the change function like this:

< select ng-model="confirmed" ng-change="change(confirmed)">
< option > 11 < /option>
< option > 12 < /option>
< option > 13 < /option>
< option > 14 < /option>
< /select>

Then, in the controller, you should modify the change function as follows:

scope.change = function(selected_value) {
    $scope.myurl = 'The result from confirmed on change: ' + $scope.confirmed;
}

That's it. Thank you for your attention and Best Regards.

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

Angular 2: Troubleshooting Issues with Observable Data Display

Looking to implement a RESTful call with Angular 2 that constantly updates whenever there are changes in the API. In my service, I've included an Observable to fetch data from the API: getData(): Observable<any[]> { return this.http.get(url) ...

I am looking to set a snapshot.val() to the $scope variable called $scope.pending and connect it to an ng-repeat list

firebase logged data Object {information: "way", status: "pending", title: "killa"} controllers.js:43 Object {information: "way", status: "pending", title: "killa"} controllers.js:42 Object {information: "way", status: "pending", title: "killa2"} control ...

Utilizing _.partial on a single-argument function

Currently, I am in the process of refactoring code within a react.js application. There is an element that utilizes Underscore.js _.partial on a function that already has one argument. Is there any real benefit to doing this? I can see how it works based ...

Is it possible to prevent the late method from running during the execution of Promise.race()?

The following code snippet serves as a simple example. function pause(duration) { return new Promise(function (resolve) { setTimeout(resolve, duration); }).then((e) => { console.log(`Pause for ${duration}ms.`); return dur ...

What is the proper way to transfer information to my ajax function from my controller?

I need to dynamically update an element on my webpage based on server-side code events. For example, when I trigger the "Start" function by clicking a button, I want the text inside a specific element to change to "Downloading", and then once the process i ...

Arranging functions in descending and ascending order

I am working on a Table component that has an array of columns and data. I need to sort the columns and update the table state accordingly. My method takes two arguments key (column key) and sortable (true or false indicating if the column is sortable or ...

Create PDFs using PhantomJS when the HTML content is fully loaded and ready

I am a newcomer to utilizing phantomjs and encountering difficulties in rendering my website as a PDF file. Although I can successfully render a simple version of the page, issues arise when multiple images and web fonts are involved, causing the DOM not t ...

Using the concept of method chaining in JavaScript, you can easily add multiple methods from

Hey there! I'm looking for some assistance with dynamically building a method chain. It seems like it should be pretty straightforward if you're familiar with how to do it... Currently, I am using mongoose and node.js to query a mongo database. ...

Send the checkbox value using ajax, jquery, and php

Hey everyone, I'm facing an issue and need some help. I am trying to send the value of a checkbox via AJAX to a PHP file. My question: I want to pass the checkbox value regardless of whether it is checked or not. If it is checked, then the value "tr ...

Is there a way to cease monitoring a value in AngularJS?

I'm currently working with the following code snippet: $scope.$watch('city', function (newValue, oldValue) { However, I need to find a way to stop the watch when I update the city list. Is there a method to remove the watch? ...

Previewing posts on a single page can be done by following a few

Hello there! I have written some code in React.js I am trying to display my blog posts on a single page when the user clicks on the "read more" button. I am fetching this data from a news API and I want to show each post based on its specific ID, which i ...

What is the best way to determine the number of characters that will fit within the width of the document?

I am looking to create a JavaScript function using jQuery that can determine the number of characters that will fit in a single line within the browser window. While I am currently utilizing a monospace font for simplicity's sake, I would like to adap ...

Utilizing React Native for seamless deep linking with automatic fallback to a webpage, including the ability to pass

I am currently working on a project that involves a website built with React and a React-native app for camera functionality and data processing. On the website, there is a button that opens the camera in the React-native app through deep-linking. This pa ...

WebRTC error encountered: Unable to add ICE candidate to 'RTCPeerConnection'

Encountering a specific error in the browser console while working on a project involving p2p video chat. The error message is Error: Failed to execute 'addIceCandidate' on 'RTCPeerConnection': The ICE candidate could not be added.. Int ...

Is it possible to employ JavaScript for performing a Ctrl-F5 refresh?

I'm working with a small asp file that runs in a frame. Is there a way to trigger a CTRL+F5 type of refresh to reload the entire browser window? I've attempted using parent.location.reload(true), location.reload(), and various other methods, but ...

Issue with JavaScript function loading website homepage solely is functioning only for the first time

I am in the process of creating my own personal website. To ensure seamless navigation, I added a "home" button that, when clicked, triggers a JavaScript function called loadhomepage() to load the homepage. While this function works perfectly upon initial ...

Use a boolean value to determine the styling of multiple items simultaneously

I'm currently attempting to modify the appearance of the bars in each area (a total of 12), so that a value of 1 equates to true (displayed as green) and a value of 0 equates to false (displayed as red). This will dynamically change the color of each ...

Is it feasible to package shared modules into individual files using Browserify?

In my web app, I am using Browserify, Babel, and Gulp to bundle my scripts into a single file. However, when I checked the file size, it was over 3MB which seems excessive to me. Although I'm not entirely sure how Babel and Browserify modify my sourc ...

NodeJS refuses to import a file that is not compatible with its structure

My website has two important files: firebase.js gridsome-server.js The firebase.js file contains JavaScript code related to Firebase integration: import firebase from 'firebase/app' import 'firebase/firestore' const config = { ap ...

Why does Internet Explorer throw a null pointer exception while Firefox does not?

My script loops through an array of HTML tag IDs, with some elements being empty. It works perfectly in Firefox but throws a null pointer or 'not an object' error in IE. if((storedVars.id) != ("")){selenium.browserbot.getCurrentWindow().document ...