working with angular forms and navigating between routes

Currently, my form consists of a basic search field where users enter a parameter and are then redirected to the corresponding page.

I am attempting to pass the argument in the form using Jade syntax:

      form(name='searchForm', ng-href='#/item/{{ctrl.search}}')
        input(type='text', ng-model='ctrl.search')

The goal is to have the route update to #/item/argument once users hit enter after typing in the search field.

Any suggestions on how to accomplish this would be greatly appreciated!

Thank you in advance

Answer №1

Check out this demo

Start by creating a dynamic route:

.when('/details/:id',

Your search function should update the location with the specified parameter:

$scope.searchFunction = function() {
  $location.path('details/' + $scope.query);
}

Retrieve the route parameter and make an API call:

$http.get('api/endpoint/' + $routeParams.id)

Answer №2

If I had to make an educated guess, I would suggest setting the action attribute on the form element. This is the traditional way for HTML forms to send data to a specific URL.

Alternatively, you could consider adding a handler to the ng-submit directive on the form itself. Within this handler, you should include something along these lines (don't forget to inject the $location service into your controller):

HTML

form(ng-submit="ctrl.onSubmit($event)")

JS (controller)

this.onSubmit = function (e) {
    e.preventDefault();
    $location.url('item/' + this.search);
}

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 the concept of returning objects in jQuery

I'm really trying to grasp the inner workings of how jQuery creates the return object when searching for DOM elements. I've delved into the source code, but I must admit that it's not entirely clear to me yet. So, I'm reaching out here ...

What are some strategies for efficiently locating partial matches in an array without having to loop through each element?

As I iterate through an array of English phrases, I check for exact matches and replace them with their corresponding translations from another array. This process is efficient and works flawlessly for exact matches. However, for partial matches, I need t ...

After the initial rendering, JQuery can dynamically search through the DOM elements and seamlessly replace keys with their respective values

I am in the process of implementing my personal localization method on my web application that is currently under development. With the use of JQuery 2.2.0 and no other frameworks or third-party tools, I need to embed certain expressions directly into my H ...

The functions .ajaxStart and .ajaxStop seem to be malfunctioning

Having trouble displaying and hiding a loading gif before and after an Ajax request using ajaxStart and ajaxStop. Here's what I have: HTML: <div id="loading" style="display: none"><img src="loading.gif></div> JS: $("#searchForm") ...

Simply modifying a custom attribute source using jQuery does not result in it being refreshed

Introduction: Utilizing jQuery.elevateZoom-3.0.8 to display zoomed-in images. The SRC attribute contains the path to the smaller/normal image The DATA-ZOOM-IMAGE attribute holds the path to the larger image used for zooming. Here is the HTML Code: ...

reCAPTCHA 2 triggers an error of 'excessive recursion'

I have decided to incorporate bootstrap 4 into my current Umbraco CMS project. My goal is to integrate reCAPTCHA 2 into a registration form, but I am encountering some difficulties in the process. I have successfully generated the keys for the reCAPTCHA ...

Troubleshooting AngularJS: Directive unable to access controller's variable within scope

I am facing a challenge with an element that has both a controller and a directive featuring an isolate scope: scope: { dirVar: '= ' } My objective is to execute specific parts of the directive only when a certain variable is true. I am try ...

How can I prevent jquery/javascript from generating duplicate list items in a jquery mobile listview?

I am currently in the process of constructing a JQuery Mobile listview by following the guidelines outlined in the getting started manual. The items on the list are being populated through json data. For the most part, everything is functioning as intende ...

Avoid the need for users to manually input dates in the Custom Date Picker

After referencing this custom Date picker in ExtJs with only month and year field, I successfully implemented it. However, I am facing an issue where manual entry into the date field is not disabled. My goal is to restrict input for that field solely thr ...

Controller is not being triggered by Ajax method when there is a decimal value

I am currently working on implementing a time registration feature in my web application. Users can select the project they worked on and enter the number of hours spent on that project. Everything is functioning properly until users start adding half-hou ...

How to use multiple template urls in Angular 6

Currently, I am creating a front-end using Angular 6 and facing the challenge of having components with varying html structures based on the user who is logged in. The number of templates required can range from 2 to over 20, so my preference would be to ...

Ways to retrieve the path of a button found within table cells

https://i.stack.imgur.com/pUYHZ.jpgI'm currently working on a table where I've created a button that's being used in various rows and tables based on certain conditions. There's a specific scenario where I need to display the button for ...

What is the process for finding GitHub users with a specific string in their name by utilizing the GitHub API

I'm looking to retrieve a list of users whose usernames contain the specific string I provide in my query. The only method I currently know to access user information is through another endpoint provided by the GitHub API, which unfortunately limits t ...

What is the best method to update the content of a bootstrap-5 popover using only pure JavaScript?

Having trouble changing the content of a Bootstrap popover using vanilla JavaScript? The code seems to be updating, but the changes are not reflecting on the site. Any suggestions or alternative methods would be greatly appreciated! Maybe there's a di ...

Instructions for downloading a .zip file sent through a HTTP response (using an axios PUT request)

When the API responds, it should contain a data property with the .zip file I need. However, the format is unfamiliar to me. The format: https://i.sstatic.net/ruaVR.png I attempted to use .blob() as suggested in similar questions on Stackoverflow, but it ...

Changing the format of a numerical value to include commas for every 1000 increment

I'm trying to find a way to format numbers in a specific manner, such as changing 1234567 into 1,234,567. However, I've run into some issues when attempting to use the currency pipe of TypeScript. It adds USD or $ in front of the number, which i ...

Desktop users will not see the mobile navigation bar

I'm working on creating a sidebar navigation that stays fixed on desktop but collapses on mobile devices. The issue I'm facing is that once I toggle the navigation on mobile, it remains toggled and doesn't show up on desktop. Check out my co ...

Unlock the Power of TWBS Ratchet: Manually Closing Modal Windows

Currently, I am in the process of developing a mobile web application using Ratchet. The main task at hand involves opening a modal, filling out a form, clicking a button to save the input data, and then closing the modal. Although I have managed to close ...

No entries found in the Nuxt/content module's array

<template> <div> <input v-model="searchQuery" type="search" autocomplete="off" placeholder="Search Articles" /> <ul v-if="articles.length"> ...

Looking for a way to conduct a recursive search in a Javascript object that fits your specific requirements?

Hello there, I'm currently developing a text-based game using Javascript. In my code, I have a variable called map which is an object containing information about different rooms in the game. I found an algorithm that I would like to modify for my sp ...