Avoid duplicating the use of the same controller in a page to prevent mirroring each other

I have implemented MVC partial controls on my page twice to handle search functionality. Each partial control has its own controller for searching, resulting in my page having two controllers with the same name.

<div ng-app="app" ng-controller="MainController" ng-init="SetSearchParam()">
     <div id="search1">
        @Html.Partial("_SearchPartial") // named search1
        // additional code for displaying search results
        // ....               
        // ..
     </div>
     <div id="search2">
        @Html.Partial("_SearchPartial") // named search2
        // additional code for displaying search results
        // ....               
        // ..
     </div>
</div>

This is the code for _SearchPartial:

<form name="SearchCommon">
    <div ng-model="search" ng-controller="SearchPartialController">
            <div>
                <input type="text" value="" placeholder="Stock" ng-model="search.Stock" />
            </div>

            <div>
                <input type="text" value="" placeholder="Make" ng-model="search.Make" />
            </div>

            <div>
                <input type="text" value="" placeholder="Year" ng-model="search.Year" />
            </div>

            <div>
                <input type="submit" value="SEARCH" ng-click="searchdata(search)" />
            </div>
    </div>
</form>

At the initialization of MainController, I set the search model value in the SetSearchParam() method as follows:

$scope.SetSearchParam = function(){
   var s = {};
   s.Make = "Test";
   s.Year = "2012";
   s.Stock = "5"

   $scope.search = s; 
};

Since the search model is utilized in the SearchPartialController and there are two search controls on the page, the value of s is set in both partial controllers. Consequently, changes made to search1 parameters reflect in search2 and vice versa.

I am seeking a solution to have the search parameters set exclusively for search1 and not affect search2. Modifications to search1 parameters should not impact search2 and vice versa.

Is there a method to accomplish this?

Answer №1

I recommend initializing your filter(search) before your function.

Controller:

$scope.search1 = { Brand : "Test", Model: "2012", Quantity : "5" };
$scope.searchData = function(search){console.log(search);};

Your view:

<body ng-controller="SearchPartialController">
    <form ng-submit="searchData(search1)">
        <input type="text" placeholder="Quantity" ng-model="search1.Quantity" />
        <input type="text" placeholder="Brand" ng-model="search1.Brand" />
        <input type="text" placeholder="Model" ng-model="search1.Model" />
        <button type="submit" class="btn btn-sm btn-primary">
                    Save
        </button>
    </form >
</body>

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

Strategies for sorting data in d3js/dimplejs visualizations

I am looking to enhance the interactivity and responsiveness of a d3js/dimplejs chart by implementing filtering based on clicks in the legends for different series. The code I tried below did not hide the series as expected, although it worked well with a ...

Unraveling the complexities of double-encoded UTF-8 in PHP

Trying to transmit data from an HTML page via Ajax to a PHP page. This is the jQuery code I'm using: $.ajax({ url: "test.php", type: "POST", data: { name: "João" } }).done(function (data) { alert(data); }) When sending ...

Node.js routing at the secondary level modifies the main directory path

In my current web project, I am incorporating both Angular JS and Node JS simultaneously. To handle routing in Angular JS, I have implemented the following code: app.config(function($routeProvider,$locationProvider) { $routeProvider .when("/expert/:exper ...

"Encountering issues with the construction of a Heroku application generated using the angular

After successfully building my app with sudo grunt build and serving it using sudo grunt serve:dist, I encountered an issue when trying to deploy to Heroku using yo angular-fullstack:heroku from the project root for the first time. The deployment process ...

Retrieve the jQuery widget instance by selecting an element within the widget

I am currently developing a widget using the Jquery widget factory. The abbreviated version of the code looks something like this: _create: function(){ this.element.hide(); //hides original input element this.newInput=$("<input>"); //creates ...

Limit not reached by substring function

When the character limit exceeds 20 characters, the substring function extracts the first 20 characters typed in the input. It replaces any additional characters that are entered after reaching the max limit of 20. In my program, however, I am able to con ...

Send all state values to the child component

I have an old application that sends a JSON to generate a multi-page form. I'm working on creating a universal multi-page form component where we can simply input a JSON to produce a form. The app utilizes a function called buildFormState which initi ...

function instance is causing confusion with the hasOwnProperty() method

When looking at the code example provided, it is interesting to note that the doOtherStuff function is defined directly on the b instance, rather than being higher up in the prototype chain (like on base or Object). This leads to a situation where b.hasOwn ...

Why is it that when implementing React JS, the function setState is not updating the values on the chart when a Button is

Currently, my webpage is built using React JS and includes a JavaScript chart. I am trying to make the data in the chart dynamically change based on a value entered into a text box. When a button is clicked, the graph should update with the new results. Ho ...

I'm trying to determine in Stencil JS if a button has been clicked in a separate component within a different class. Can anyone assist

I've created a component named button.tsx, which contains a function that performs specific tasks when the button is clicked. The function this.saveSearch triggers the saveSearch() function. button.tsx {((this.test1) || this.selectedExistingId) && ...

Ways to determine if JavaScript array objects overlap

I am working with an array of objects that contain start and end range values. var ranges = [{ start: 1, end: 5 }] My goal is to add a new object to the array without any overlapping with the existing ranges, { start: 6, end: 10 } I need ...

What events precede and follow a keydown action in a Textarea field?

I need to prevent users from pressing function keys (F1, F2, etc.), the tab key, and any other characters from being added. The code below is supposed to achieve this on my website but it's not working. document.getElementById("code").addEventList ...

React Functional Component fails to update on state changes

I'm in the process of creating a React application where I can input my height and weight to calculate my BMI. The goal is to display the BMI value on a diagram. To keep things organized, I decided to break down the functionality into smaller componen ...

How can contextual binding be implemented in TypeScript?

In Laravel, you have the option to specify which class should be imported if a particular class is accessed. For example, if someone tries to use the Filesystem contract, it will return the Storage Facade (Laravel Contextual Binding). Similarly, if someo ...

Guide to organizing a one-to-one object map within Angular JS ng-repeat

Is there a way to organize a one-to-one object map in angular.js using filters (or any other technique) while working within an ng-repeat loop? This is what I currently have: obj:{ a:3, c:5, d:1, g:15 } Basically, I want to achieve s ...

Refresh information in form after submitting with Remix

Currently, I am utilizing the Remix Form element to display my form with various input fields. When there is a server-side validation error, the entered text in the fields remains, as expected. However, upon successful submission of the form, I would like ...

Dropzone JavaScript returns an 'undefined' error when attempting to add an 'error event'

When using Dropzone, there is a limit of 2 files that can be uploaded at once (maxFiles: 2). If the user attempts to drag and drop a third file into the Dropzone area, an error will be triggered. myDropzone.on("maxfilesexceeded", function(file){ a ...

VueJS not refreshing DOM after AJAX data modification

Utilizing Vue.js to make changes to my DOM, I have implemented the fetch_data() method. This method attempts to update data.messages to display 'Love the Vue.JS' once the AJAX call is successfully completed. The AJAX call executes successfully a ...

Ways to showcase the outcome of a spin after each rotation

I am currently working on a spinning code snippet that allows users to spin and potentially win different amounts. The code includes functionality to prevent more than 3 spins, set random rotation values, and animate the spinning effect. However, I now wa ...

I'm struggling to make this background show up in a div

Anyone able to help me figure this out? I can't seem to get the achtergrond_homepage.png as a background in rounded corners. Edit: It seems like the gray color is always on top. Could it be controlled in the JavaScript part? This is the CSS: @ch ...