How can I use an AngularJS directive to choose a specific item from a list?

Among my plethora of templates, I'm seeking a way to handpick one from the list and transfer its ID to an input field within a form. Here's how my setup appears:

<form>
    <input type="hidden" name="template_id" ng-model="template.template_id" />
    <input type="text" name="template_name" ng-model="template.template_name" />
    <ul>
        <li id="1">Template1</li>
        <li id="2">Another Template</li>
    </ul>
    <button type="submit"></button>
</form>

My goal is to dynamically change the content in the inputs by clicking on any of the <li> elements. Would it be possible to achieve this functionality using a directive? Appreciate your insights! - Daniel.

Upon selecting the first item from the list, I envision the input fields with template_id and template_name to reflect Values 1 and "Template1" respectively. Similarly, picking the second item should adjust template_id to 2 and template_name to "Another Template".

Answer №1

Give this a try:

for HTML :

     <ul>
         <li ng-repeat="item in itemList" ng-click="selectItem(item)">{{item.name}}</li>
     </ul>

in the Controller:

  $scope.itemList = [{id:1, name: 'Item1'}, {id:2, name: 'Another Item'}]

  $scope.selectedItem = {};
  $scope.selectItem = function(item) {
    $scope.selectedItem.item_id = item.id;
    $scope.selectedItem.item_name = item.name;
  }

VIEW DEMO

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

Utilizing lodash to Filter Arrays Within Arrays

Let's take a look at the JSON structure provided below. comapany : ABC Emp Info:[ { empName: D, empID:4 salary[ { year: 2017, ...

angular global search is only effective without any filters applied

I am facing an issue with my global search function. It works fine when I display the data directly from the controller. However, the problem arises when I apply a filter - the search function no longer detects the data. For example, if I have a date time ...

Error: React Select input control is throwing a TypeError related to event.target

Having trouble changing the state on change using a React Select node module package. It works with regular text input, but I can't quite get it to work with this specific component. The error message "TypeError: event.target is undefined" keeps poppi ...

The Vue component fails to refresh after modifications to the state in the Pinia store

I'm currently immersed in my inaugural vue application, focusing on constructing the login functionalities. To handle State management, I've implemented pinia. I've set up a Pinia Store to globally manage the "isLoggedIn" state. import { def ...

Using a single name to target multiple input fields

I currently have multiple input fields with prices: <input type="text" name="price[]" value="100"> <input type="text" name="price[]" value="50"> <input type="text" name="price[]" value="10"> <input type="text" name="price[]" value="90 ...

Upgrading to React Router v6: Implementing Loader Functions with Context API

Having issues implementing loaders in React-Router V6 while making a request for a page through a function located in the context file. Unfortunately, I can't access the context from main.js where the router is defined. Main.js import ReactDOM from & ...

Ways to Retrieve Material-UI Date Picker Data

I am struggling to figure out how to set the value selected by the user in a material-ui datepicker to state. Any help would be greatly appreciated. Currently, this is what I have been trying: The datepicker component code looks like this: <DatePicke ...

Utilizing jQuery to execute a consistent action for every element on a webpage

After reading numerous related questions, I have come to the conclusion that none of them seem to address my specific situation. I am looking to add a border around a single element without affecting its parent elements. My attempt with the code below usi ...

The asynchronous functionality for reading and writing files from 'fs' is not functioning properly

I've been working on a script that reads and writes files, and so far the functions are functioning properly. However, I am facing challenges in making the read/write functions asynchronous. The main issue arises when trying to write a file and then i ...

The button labeled "modify layout" remains unresponsive when activated

Allow me to start by saying that I am not a coding expert. Currently, I am enrolled in a computer science class and have a basic understanding of HTML, CSS, and some Javascript. However, I have encountered a problem that I cannot solve. When I click on th ...

utilizing angularjs and bootstrap to manage multiple button models

Recently delved into learning angularjs and bootstrap. Found a tutorial on creating buttons. <div ng-controller="ButtonsCtrl"> <h4>Checkbox</h4> <pre>{{checkModel}}</pre> <div> <button type="butto ...

Explore all potentialities within an array of objects by examining and contrasting their key values

Looking to run a specific math formula with three parameters using an array of objects in JavaScript. The scenario involves sports, where there are three possibilities: Team A (win), Team B (win), or Draw. There are three different bet websites offering o ...

undefined value being returned by a mongoose schema function

I'm working on a method to validate a user's password using bcrypt.compare(). See the code snippet below for details. UserSchema.methods.validatePassword = async (data) => { console.log(this.email); // returns undefined console.log(this.fi ...

Comparison between Ionic framework and Famo.us

Can you explain the distinctions between Ionic Framework and Famo.us Framework? Is it feasible to utilize both frameworks in a single project? Which one offers better graphic rendering on mobile devices? Do both frameworks have support for AngularJs? ...

Incorporate the teachings of removing the nullable object key when its value is anything but 'true'

When working with Angular, I have encountered a scenario where my interface includes a nullable boolean property. However, as a developer and maintainer of the system, I know that this property only serves a purpose when it is set to 'true'. Henc ...

Exploring the React component life cycle: Understanding the distinction between render and return, and what happens post-return

This question pertains to the concepts surrounding react component life cycles. Below is an example code snippet provided as a general reference. const Modal = ({ className, variant, width, withCloseIcon, isOpen: propsIsOpen, onClose: tellParen ...

Encountering issues with installing packages while creating a new Angular 9 project

Recently I updated to node version 12.16.1 (LTS) and Angular CLI version 9.0.3. After creating a new project with the CLI, all files in the root folder are generated but it gets stuck during the installation of node packages. Has anyone else encountered t ...

Sending an POST request using AJAX to a Web API Controller while ensuring CSRF protection

In order to prevent Cross Site Request Forgery in my Web API Controller within an MVC application, I am seeking different methods for protection. Currently, I have an MVC View that showcases an Esri map using the ArcGIS for JavaScript API. Users are able t ...

The Ajax code fails to refresh the document object model

I've encountered a small issue with my Ajax script that performs a POST request and displays the result on the screen. Even though the POST request is successful, I'm not able to view the result on the screen. It seems like there might be a need ...

Issue with CoffeeScript and three.js: scene not defined

I've been troubleshooting this issue for hours, but I can't seem to figure out the error... Here's the error message I'm getting: Cannot read property 'add' of undefined Below is my coffeescript code file (hopefully it&apos ...