The AngularJS filter may encounter issues if the filter's model is located in a separate view that shares the same controller

I am facing an issue with using a filter in my second view while having two views that share the same controller "TabsController as TabsVM". The filter I need to use is "item in items | filter:modelName", where modelName is located in the input field of the first view. Strangely, the filter doesn't work when the input field is in a different view from where the filter is applied. Is there a way to make it work without merging these views?

UPDATE:

I have attempted multiple solutions but have been unsuccessful in connecting the models in the filter.

first_view.html

<nav class="navbar navbar-default navbar-fixed-top" role="navigation" ng-controller="NavigationBarController as NavBarVM">
        .......
        .......
    <input type="search" class="form-control" ng-model="NavBarVM.searchInputFieldText" ng-change="NavBarVM.updateSearchInputValue()"/>
        .......
        .......
</nav>

first_view.js

app.controller('NavigationBarController', ['$scope', 'NewProjectService', function ($scope, NewProjectService) {

    var vm = this;

    NewProjectService.searchInputFieldText = vm.searchInputFieldText;

    vm.updateSearchInputValue = function()
    {
        NewProjectService.updateSearchInputFieldText(vm.searchInputFieldText);
        NewProjectService.searchInputFieldText = vm.searchInputFieldText;
    };
}]);

second_view.html

<div class="col-lg-8 col-md-8 col-sm-8 testTabsArea" ng-controller="TabsController as TabsVM">
        .......
        .......
    <div class="panel panel-default" ng-repeat="panel in TabsVM.panels | filter:TabsVM.searchInputFieldText">
        .......
        .......
</div>

second_view.js

app.controller('TabsController', ['$scope', 'NewProjectService', function ($scope, NewProjectService)
{
    var vm = this;
    vm.searchInputFieldText = NewProjectService.searchInputFieldText;
    ............
    ...........
}]);

service.js

app.service('NewProjectService', function(){
...........
...........
    var searchInputFieldText = '';
    var updateSearchInputFieldText = function(text)
    {
        searchInputFieldText = text;
    };
    return{
        updateSearchInputFieldText: updateSearchInputFieldText,
        searchInputFieldText: searchInputFieldText,
    };

});

Answer №1

A factory function called Controller is utilized to return an object, resulting in the creation of two separate objects when the controller is called twice within your scenario.

Source: https://docs.angularjs.org/guide/controller

Update:

Consider modifying

<div class="panel panel-default" ng-repeat="panel in TabsVM.panels | filter:TabsVM.searchInputFieldText">

to

<div class="panel panel-default" ng-repeat="panel in TabsVM.panels | filter:NavBarVM.searchInputFieldText">

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

The Vue ChartJS fails to display properly after the page is refreshed

Although this issue may appear to be a common one, some of the suggested solutions are outdated or no longer functional. My objective is to ensure that the chart remains responsive even after the page reloads. I attempted to implement the solution provided ...

Using async/await with recursion in NodeJS for handling express requests

My code contains a function named GetAllData() that triggers the GetPurchaseData function, which in turn calls itself recursively until all the data is loaded. async function GetAllData(){ console.log("starting loading purchase data"); await G ...

Mastering the art of simultaneously running multiple animations

Utilizing two functions to apply a Fade In/Fade Out effect on an element. Confident in the functionality of both functions, as testing them sequentially in the Console proves their effectiveness. However, executing: fadeIn() fadeOut() seems to result in ...

What causes the erratic behavior of zooming on my website when using mobile devices?

My website is experiencing difficulties on mobile devices despite my efforts to enable zooming. I have tried various methods, including experimenting with meta tags like user-scalable=1, user-scalable=true, and adjusting min and max values, but the issue p ...

Converting a multi-dimensional array from PHP to JavaScript in a way that cannot be accessed using

I have been working on passing a multidimensional array from PHP (with dynamically inserted values in a real scenario) to JavaScript. However, I am facing an issue where the values are not being printed when using a loop. Upon checking, the array[0].length ...

Issue with Jquery forms extension

Below is the code snippet I am using: $(document).ready(function(){ // bind 'myForm' and provide a simple callback function $('#form').ajaxForm(function() { alert("Works!!!"); }); ...

How can you implement code that responds differently based on whether the user clicks the OK, cancel, or close button in a confirm dialog

What are three buttons in a confirm dialog box? How can JavaScript be used to perform different actions when the "ok" and "cancel" buttons are clicked, without performing any action when the dialog box is closed? ...

Troubleshooting Tooltip Problem on Highcharts US Bubble Map

Using Highcharts in my React application, I have built a US Bubble Map. The version details for the libraries used are: "@highcharts/map-collection": "^2.1.0" "highcharts": "^11.1.0" "highcharts-react-official& ...

Exploring Json parsing in Python with Django

I am currently using Django for my web application. I am facing an issue where I cannot access Nodes and edges by calling decoded_data['nodes']. Instead, I am encountering the error message: 'NoneType' object is not subscriptable Thi ...

jQuery's find method returns a null value

During my Ajax POST request, I encountered an issue where I wanted to replace the current div with the one received from a successful Ajax call: var dom; var target; $.ajax({ type: "POST", url: "http://127.0.0.1/participants", data: "actio ...

Storing retrieved data from an Ajax request into a variable

Presented below is a function that utilizes Ajax: function fetchSource(callback){ var ajaxRequest; try{ ajaxRequest = new XMLHttpRequest(); } catch (e){ try{ ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP"); ...

In NodeJS, inserting array objects into GraphQL is prohibited

Encountering an issue while trying to insert array objects. Below is the code snippet: GraphQL Schema type Member { _id: ID! member_id: Int! first_name: String! last_name: String username: String date: String } input MemberInput { member_i ...

Tips for deactivating a single edit button

Is there a way to make it so that when I click on a checkbox, only that specific todo's edit button is disabled? Currently, clicking on a checkbox disables all edit buttons in the todo list. Any suggestions? class App extends Component { state ...

The error message "TypeError: task is not a function" is being

I encountered an issue while attempting to make a post request from Postman using the URL localhost:3000/todos. The request resulted in a 500 internal server error, and I also received an error stating that 'todo' is not a function. Here is the c ...

Setting up parameters and arguments in Vuex mutations: A guide

I am currently developing a todo list application using Vue.js, Vuex, and Firebase. The functionality of the app seems to be in working order with the Store file effectively managing the retrieval and display of entered todo items to and from Firestore. Ho ...

I could see the Javascript and CSS code manifesting directly onto my HTML page

I've encountered a strange issue with my calendar code where my JavaScript and CSS codes are inexplicably showing up next to the calendar. Does anyone have any insights into why this may be happening? I developed the code in an online HTML/CSS/JS edit ...

Submitting the information through a for loop for every array within the nested array using Ajax

My data consists of a Sensor and multiple Alarms related to this Sensor. I am attempting to create several Alarms using a for loop, and then I want to use these Alarm variables in the Sensor JSON object. However, I am facing difficulties with this process. ...

What is the process for passing external JSON data to a task from a file that was generated in a previous task?

Currently facing an issue with the following setup in my Gruntfile: grunt.initConfig({ shell: { // stub task; do not really generate anything, just copy to test copyJSON: { command: 'mkdir .tmp && cp stub.json .tmp/javascript ...

Troubles with side menus causing issues with a tabbed interface on Ionic

I have a unique sliding menu application where each menu item opens up a view to populate data. Everything is functioning well, but in one specific view, I am trying to incorporate an Ionic pager with slides. It seems that the sliding menu is interfering w ...

Creating JavaScript accessors for Java beans

Is there a Java framework available that can automatically generate JavaScript code to access backend beans? For instance, suppose I have a Spring service named TestService: public interface TestService { public static class UserDTO { public S ...