Having trouble with bidirectional data binding in AngularJS directive with isolate scope

After attempting to update the list using a directive template, the data is not being updated following an HTTP request.

test.html:

<div ng-repeat=" comment in [{name:"A"},{name:"B"},{name:"C"}]">
    <div lookup-product-icon lookup="lookupProduct(comment)"   products="products"></div>
</div>
<div lookup-products></div>
....

Directive:

var app = angular.module('myApp');
app.directive('lookupProductIcon', function() {
    return {
        scope : {
            lookup : "&"
        },
        template : '<div  ng-click="lookup()">Use me!</div>',
    };
});

app.directive('lookupProducts', function() {
    return {
        restrict : 'EA',
        transclude : false,
        scope : {
            products : '='
        },
        templateUrl : 'lists.html'
    };
});

Controller

$scope.products = [];
        $scope.lookupProduct = function(lists) {
                var details = {
                name : lists.name,
                host : $scope.host_name
            };
            productService.lookupProduct(details).then(function(data) {
                $scope.products = data.list;
                console.log($scope.products);
                //Here display the lists in developer tools.
            }).catch(function(data) {
                if (data.message) {
                    alert(data.message);
                }
            });

        };

List.html:

<ul>
  <li ng-repeat = "product in products">    {{product.name}}    </li>
</ul>

When clicking "Use me!", an HTTP request should be sent and the response should update the lists in list.html.

The lookupProduct function works correctly, but the issue lies in the products not updating.

Details:

I have included two directives: 1. lookupProductIcon - This displays text which, when clicked, triggers an HTTP GET request for updating the data in list.html (lookupProducts directive). 2. lookupProducts - The data update does not occur here.

Answer №1

Make sure to include the scope variable products when using the lookupProducts directive in your HTML code.

It is essential to pass an array of products to the lookupProducts directive for it to function correctly.

<div lookup-products products="products"></div>

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 steps are involved in creating a function within AngularJS?

Just starting out with AngularJS and looking to implement a shopping cart using WebSQL. Wondering how to create functions for adding items to the cart and removing them. Here's the code snippet I have so far: angular.module('ecommerce').fa ...

Switching Between HTML Using Javascript

I am currently working on an app that allows users to easily check the local weather and temperature in either celsius or fahrenheit. However, I've encountered a problem when trying to switch between the two unit types by simply clicking on the temper ...

The Twilio API is failing to deliver the response in JSON format

I have been working on an API to send WhatsApp responses to clients using Twilio as the service provider. However, I am encountering a problem where I am unable to receive the response in JSON format, despite following the code examples provided by Twilio. ...

Load data from a MySQL Database into a DataTable

I am currently in the process of developing a CRUD application. With a large dataset stored in a MySQL database, my intention is to utilize a JQuery DataTable instead of manually creating a table. However, the issue I am facing is that while the table appe ...

The Access-Control-Allow-Origin CORS header does not align with the null value on the Ajax request

Encountering the same issue in my browser Cross-Origin Request Blocked: The Same Origin Policy prevents access to the remote resource at http://abc-test123.com/login. (Reason: CORS header ‘Access-Control-Allow-Origin’ does not match ‘(null)’). My ...

Tips for Adding Items to an Infinite Loop

Could you please review This Demo and advise me on how to continuously load items into the ul list in an endless manner? Currently, I have this code set up to display the list but I want it to keep adding new items to the end every time. var item = $(". ...

Utilizing JQuery Mobile AJAX to load JSON data

Hey there, I'm currently diving into the world of developing my first mobile app for my WordPress blog. One of the key components I've set up is the JSON API plugin on my WordPress site, allowing me to access my JSON Data via "example.com/api/get ...

What is the maximum duration we can set for the Ajax timeout?

I am facing a situation where an ajax request can take between 5-10 minutes to process on the server side. Instead of continuously polling from JavaScript to check if the request is completed, I am considering making just one ajax call and setting the tim ...

Tips on incorporating `.env.local` into a script in `package.json`

Within my /src directory, I have several files named .env.*: .env .env.local .env.staging The content of src/.env is as follows: REACT_APP_NODE_ENV=123 The content of src/.env.local is: REACT_APP_NODE_ENV=456. To address this, I decided to install ...

Using Three.js : THREE.VTKLoader() to assign an ID for an event

I have been attempting to load multiple .vtk files using a for loop in the following manner: for (var i = 0; i < vtkFilesArray.length; i++) { var loader = new THREE.VTKLoader(); loader.load( i+".vtk", function ( geometry ) { console.lo ...

Add a new key-value pair to each object in an array, with the value generated by making a request using axios.get

Recently, I've been working on a scraper that has been functioning well until I encountered an issue while trying to scrape data for individual links. Let me clarify the situation: The scraper gathers data about apartments from a webpage where articl ...

What is the best way in Angular to focus on an input field using its name, model, or id?

My goal is to create a form where, upon leaving field one (blur), the system will check if the data inputted is the word "test". If the data does not contain this word, I want the focus to return to field 1. <form name='yourForm' novalidate n ...

Tips for smoothly applying a class to an image for seamless transitions, avoiding any awkward clunkiness

Check out my work on jsfiddle I understand that the image may not be visible, but I'll do my best to describe it. The header smoothly animates as I scroll down, but the image abruptly changes size instead of smoothly resizing. I want it to gradually ...

Storing information in a MongoDB database using Node.js

Context: Looking to insert data into a MongoDB database using Node.js Problem Statement: Attempting to insert data into the MongoDB database but encountering an error. Unable to locate the issue. Present Output: Reference Error Attach Code: filter.js ...

Display event using Angular

I am currently working on an Angular project where I need to print a specific area of the page. While I know that this can be done using media CSS, it is causing issues for me due to the numerous print functionalities present in the project. I am attemptin ...

How can I save the indexed directory to a variable using Node.js?

What is the best way to index a directory in nodeJS and save it as a variable? I haven't made any attempts yet, so I'm not sure where to start with the code. ...

Encountering null when making an AngularJS http post request within CodeIgniter

Currently, I am facing an issue with user registration using AngularJS and Codeigniter as I keep receiving a NULL response in the POST request. $scope.signup=function(user){ $http({ method : 'POST', url : baseUrl+&apos ...

Angular-ui does not support the ng-disable directive

<h3 class="pulse-green-text"><span class="icon ico_pulse_download"></span>VPN Certificate</h3> <div class="vpn-cert" ng-controller="vpnController vpnCert"> <form method="post" name="userform" class="form-horizontal" id="u ...

Is it possible to dictate a custom sequence of operations in THREE.js?

Check out this question on Stack Overflow regarding Threejs Transform Matrix ordering: Threejs Transform Matrix ordering I'm currently working on depicting the orbits of planets and I've encountered some difficulties when trying to change the in ...

The highlight_row function seems to be delayed, as it does not trigger on the initial onClick but works on subsequent clicks. How can I ensure it works properly right from the first click?

I developed an application using the Google JavaScript Maps API to calculate distances between addresses. I've encountered a bug where the row in the table is not highlighted on the first click, requiring a second click for the highlighting to take ef ...