I am facing a challenge in retrieving the chosen item from a drop-down menu on my subsequent page using AngularJS

Here is the code snippet from my app.js file:

var countryApp = angular.module('countryApp', ['ngRoute']);
countryApp.controller('testController', ['$scope', '$http', '$location', function ($scope, $http, $location) {
  $http({
    url: 'countries.json',
    method: "GET",
    headers: {'Content-Type': 'application/json'}
  }).success(function (data) {
    $scope.details = data;
    console.log("data---------->", $scope.details);
  }).error(function (data) {
    console.log("data error---------->", $scope.details);
  })
}]);

countryApp.config(['$routeProvider', function ($routeProvider) {
  //$httpProvider.interceptors.push('httpErrorInterceptor');
  $routeProvider.when('/countrydetails', {
    templateUrl: 'partials/country-detail.html',
    //controller: 'testController',
  }).otherwise({
    redirectTo: '/countrylist',
    templateUrl: 'partials/country-list.html',
    controller: 'testController',
    });
}])

countryApp.factory('testService', ['$scope', '$http', function ($scope, $http) {
  return { }
}])

This is what I have in my countries-list.html file:

<h1>COUNTRIES</h1>
<select>
  <option ng-repeat="country in details | orderBy:'name'">
    <a>{{country.name}}</a>
  </option>
</select>
<a href="#/countrydetails"><button >click</button></a>

I am trying to capture the country name on the next page when the button is clicked. To achieve this, I have a separate HTML page called country-details.html. How can I retrieve the country name in this HTML page?

Answer №1

Create your navigation path accordingly

$routeProvider.when('/countrydetails/:name', {
templateUrl: 'partials/country-detail.html',
});

Revise your html code.

<a ng-href="#/countrydetails/{{country.name}}">
<button >click here</button></a>

It is recommended to connect to a controller function for a more efficient route handling, but for now this setup should suffice for your needs.

Answer №2

Here is a Plunker that I created to mimic your specific situation. Take a look and see if it provides any assistance. http://plnkr.co/edit/hV16OB?p=preview

It's crucial to pay attention to this particular segment of code.

$routeProvider.when('/countrydetails/:countryName',

Answer №3

To create a dynamic href, follow this format:

<a href="#/country-info/{{contry.name}}">

Specify the route as

$routeProvider.when('/country-info/:name', {

In the Controller for the subsequent page, include $routeParams

controller('CountryInfoController', function ($scope, $routeParams) { 
   $scope.selectedCountry = $routeParams.name
});

In your HTML, you can display the country name using {{selectedCountry}}

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

Interactive table with ui-if functionality

Take a look at this code snippet: 1. <th ui-if="testBool">Test</th> The issue with this code is that it only includes or excludes the value 'Test', rather than the entire table header. 2. <div ui-if="testBool"> <th> ...

Navigating Information in the Response - Node.js

Currently, I am working on a node.js project that has grown significantly with numerous endpoints. As the project continues to expand, modifying the data within the responses for each endpoint has become quite cumbersome. I find myself constantly navigatin ...

Display a container using Fancybox

With just a click of a button, I want to reveal a hidden div in an elegant box. Here's the code snippet that actually works: $("#btnForm").fancybox({ content: $("#divForm").html() }); However, after doing some research, it appears that this may not ...

Using AngularJS to create a numeric input with a non-numeric model

I've encountered an error message related to an input field that is set as type "number" while the actual model is a string. The reason for this discrepancy is that my ngModel value is utilized in other areas of the application. However, for a specif ...

Can a client component in NextJs retrieve data from a server component?

Apologies for the way the question is phrased. I have a server component named auth.ts that retrieves user details after they log in. This server side component is located at //auth.ts export const validateRequest = cache( async (): Promise< { use ...

A more efficient method in jQuery to detach and re-attach elements

Apologies for not having a more creative title. I am interested in finding a better, more concise way to write the function below. Specifically, I would like to simplify the process of moving elements around. The function is designed to create a parent di ...

Internet Explorer 11 refuses to acknowledge Angular JS variable

While this code functions properly in Chrome, it does not display the background color when viewed in Internet Explorer 11: span.fa.fa-icon-only(style='background-color: #{{vehicle.visual_color}};', rr-tt='{{vehicle.color_name}}') It ...

When working with ng-show and ng-hide in AngularJS, I noticed a brief 1ms delay where both objects are still

I am encountering a similar issue as discussed in the following post: How to use ng-show and ng-hide with buttons in AngularJS In my case, I have play/pause buttons where the ng-show/hide functionality is working. However, the problem lies in the fact tha ...

Did the menu get shifted downwards on the mobile version by using bootstrap?

Here is the desktop version of a navigation bar. https://i.sstatic.net/e595L.png This image shows the mobile version after clicking on the hamburger button. https://i.sstatic.net/ng1tK.png I would like the menu to open when I click on the hamburger, bu ...

Why using $refs in interpolation fails and leads to errors in Vue.js 2.x components

Here is a codepen I created: codepen const sidebar = { name: "sidebar", template: "<p>SIDEBAR</p>", data() { return { active: true }; }, methods: { test() { alert("test: " + this.active) } } }; new Vue ...

Retrieve the ultimate information from the Angular service

Within my angular 6 project, I am dealing with a product_id array, product_id: any = ["123", "456"]; ngOnInit : ngOnInit() { this.product_id.forEach(element => { this.httpClient.get('https://api.myjson.com/bins/hiolc').subscribe ...

Only when the tab is refreshed will React Query be shown

I'm facing a tricky situation and can't seem to find a solution through Google search. My current challenge involves using the React Query library with TSX to display fetched data in a simple list. However, the data is only fetched and displayed ...

Why might the MIME type 'application/json' be refused when the dataType is JSON?

Is it true that if the dataType is set to JSON in jQuery, it expects a JSON response? I found http://api.jquery.com/jquery.ajax/ to be informative on this topic. In the following function, I have experimented with specifying both no dataType (for jQuery&a ...

Issue with animated cursor function not activating when used with anchor links

I've spent hours searching for a solution but I can't seem to find one. I'm attempting to modify the codepen found at https://codepen.io/Nharox/pen/akgEQm to incorporate images and links, however, two issues are arising. The first issue is t ...

Slideshow featuring a dynamic ken burns effect with color overlays

I am having an issue with my ken burns effect slideshow on the mobile site. I am trying to add an overlay that automatically switches between green, red, yellow, and blue colors but it's not showing up. I can't seem to figure out what the problem ...

Is the unit-converts npm package compatible with the website https://unpkg.com?

Having issues importing the npm package 'convert-units' using unpkg, I attempted the following method: <script src="unpkg.com/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="02616d6c746770762f776c6b767142302c31 ...

Are there colons present in the JSON data object responses?

I'm working with a JSON response that has the following structure: items |___ [0] |____ media:group |______media:thumbnail |_______ [0] |_ ...

Are there any publicly accessible APIs available to retrieve data values based on the file type?

Currently, I am working on a project that requires uploading and downloading files. The current functionality allows only .csv and .txt file types to be downloaded using the code snippet below: downloadFile(file).then( function (response) { va ...

What is the best way to create a new instance for each incoming HTTP request in NestJS?

While working with my API, I encountered an issue where the classes were not being destroyed after receiving a response. Currently, I am using nestJS, but when I tested nodeJS + expressJS, I also faced the same problem. The code I am using is as follows: ...

AngularJS provides several ways to redirect users to different views within a

Although it may seem simple, I am new to angularjs. Here is the code for my angularjs controller: function MyCtrl1($scope, $location, $rootScope) { $scope.$on('$locationChangeStart', function (event, next, current) { event.preventDefau ...