Values of inputs are being lost due to filtering from their parent element

Essentially, I am dealing with a directive that represents a site and has multiple input fields for data collection. With potentially hundreds of these directives on the page, I have implemented a search function to quickly locate a specific site.

However, my issue arises when using the search feature - filtered out items lose all the entered data instead of simply hiding. I expected the filtered items to retain their values but it seems like they are being re-rendered from scratch. Am I missing something or is there a better way to handle this?

Check out a simple Plunker demonstration of my problem here

The HTML structure:

<body ng-controller="MainCtrl">
  <input type="search" ng-model="q" placeholder="filter sites...">
  <my-directive ng-repeat="site in sites | filter:q" site="site"></my-directive>
</body>

The JavaScript:

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

app.controller('MainCtrl', function($scope) {
  $scope.sites = [
    {name: 'site 1', id:'site1'},
    {name: 'site 2', id:'site2'}
    ];
});

app.directive('myDirective', function($compile) {
  return {
    restrict: 'E',
    scope: {
      site: '=',
    },
    template: '<h1>{{site.name}}</h1>Value: <input name="{{site.id}}">'
  };
});

Answer №1

Your input values are not linked to the model, meaning that when Angular refreshes the UI, new elements are created and any previously entered data is not saved unless you use ng-model or events to retain the values.

Give this a shot:

template: '<h1>{{site.name}}</h1>Value: <input name="{{site.id}}" ng-model="site.SomeProperty">'

Check out the updated Plunker here

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

What is the method for modifying the array that has been generated using Vue's "prop" feature?

According to the Vue documentation, a prop is passed in as a raw value that may need transformation. The recommended approach is to define a computed property using the prop's value. If the "prop" is an array of objects, how can it be transformed int ...

The x-axis scale in d3 is malfunctioning

I'm categorizing data into groups such as rich, poor, and all. I'm using a dropdown to select these values to be displayed on a scatterplot. The first transition works correctly, with text labels appearing as expected. However, upon selecting ano ...

What might be the reason my jquery counter function is not functioning properly?

Hi there, I'm currently working on creating a counter for my website. However, I'm not very proficient in JavaScript and seem to be encountering some errors in my code. Would anyone be able to assist me in writing a piece of code that functions c ...

An efficient method to eliminate an element from a linked list

In a TED talk by Linus Torvalds, he demonstrated both "poor" and "good" programming practices using an example of removing an item from a linked list. First, the poor taste example: https://i.sstatic.net/pgZTo.png Now, the good taste example: https://i ...

Struggling to find a solution for changing the color of a select box when an option is chosen

Here's an example of the HTML I'm working with: <select onclick="colorchanger()"> <option name="white" value="0">--Select--</option> <option name="red" value="1">Work</option> <option name="green" value="2" ...

Having trouble with passing props to data() in Vue.js?

I'm facing an issue where I am unable to pass props to data() in my Vue inline template: <network-index inline-template> ... <network-list :data="networks"></network-list> ... </network-index> Inside the Index.vue file, here ...

Using Confluence to Access a Unique REST API

I've successfully developed and deployed my own webservice using WCF C#. Now, I'm looking to utilize JavaScript to call this service, retrieve data from it, and display it on a chart. Below is the code snippet that I placed within a custom HTML ...

Leverage the inherent capabilities of jQuery to seamlessly transmit form data via ajax using the pre-existing

Snippet of Code: <?php if ($_SERVER['REQUEST_METHOD'] == 'POST'){ header('Content-Type: application/json'); echo json_encode($_POST); exit(0); } ?> <html> <head> <titl ...

Learning how to use Express.js to post and showcase comments in an HTML page with the help of Sqlite and Mustache templates

I am facing a persistent issue while trying to post new comments to the HTML in my forum app. Despite receiving various suggestions, I have been struggling to find a solution for quite some time now. Within the comments table, each comment includes attrib ...

Steps for removing an embed after a set time period

I've been using message.delete({timeout: 3000}) to remove messages with the prefix. Now I'm wondering how I can also delete the embed I sent after a certain amount of time. if (!args[0]) return message.channel.send({ embed: { color: 1677720 ...

Node.js process.exec() function allows you to asynchronously spawn a subprocess

After writing the code, I ran it and found that the terminal was unresponsive with no output, causing the program to be stuck. var util=require('util') var exec=require('child_process').exec; exec('iostat 5',function(err,stdo ...

I am encountering an error while attempting to use the app.post request and it is not

I've been struggling to create the server-side back-end code for my website. I tested an app.get request in Postman and it worked fine, but when I attempted an app.post request, it failed and returned errors. I've tried all the solutions found ...

What could be causing the npm mysql module to malfunction when trying to initiate the 'connect()' function in a separate .js file?

When I call require('mysql') and use the function connect() everything works fine. However, if I try to call the 'connect()' function in another file, it throws an error saying connection.connect is not a function... Any suggestions on ...

"Executing ng-click directive on a second click changes the route in AngularJS

Why does my ng-click only redirect to a new location on the second click? It should redirect to $location.path('/login.signin');, but it only works if I click the button again. It's strange. Where could this issue be originating from? Belo ...

Steps to convert a phone number into JSON format

The primary focus Upon receiving an MQTT packet, it is displayed as an ASCII array in the buffer after being printed using stringify: packet = { "cmd": "publish", "retain": true, "qos": 1, "dup& ...

Strange ASP.NET Redirection/Submission Problem

My code is enclosed in a try/catch block with a redirect to another page, but I'm encountering a problem. When a user clicks on a submit button, instead of redirecting to the intended page, the page simply refreshes and stays on the same page. This is ...

A dynamic context menu using jQuery within AJAX-loaded content

After spending hours searching for a solution without success, I am turning to this forum for help (I have come across similar questions but none of them have solved my issue). The problem I am facing is with implementing a custom context menu on a page t ...

Utilizing cascading dropdowns to automatically populate fields in a Knockout MVC

Currently, I am facing an issue on the Update details screen with a Country and State dropdown. My goal is to pre-populate the State dropdown based on the selected Country. Initially, I have access to the selected Country, Country Collection, and Selected ...

I am dynamically generating table rows and populating them with data. However, I encountered an issue with retrieving the data by their respective IDs

Creating a table row dynamically by populating it with data. However, encountering an issue with retrieving the data by their respective id. if(xmlhttp.status == 200) { var adminList = xmlhttp.responseJ ...

What is the best way to set up playwright-jest so that I can skip a specific Test Suite (spec) file during execution?

In my repository, I have a setup using `playwright-jest` with multiple spec files for test suites. I need to skip one of the spec files without moving it from its current directory in the repo. The default script in `package.json` is `"test": "jest -c jes ...