ES6 does not support two-way binding functionality

Let's take a look at this snippet of code:


export class TestController {
    constructor() {
        this.socket = io();
        this.movies = {};
        this.socket.emit('getAllMovies', '');
        this.socket.on('allMovies', this.listMovies.bind(this));
    }

    listMovies(data){
        this.movies = JSON.parse(data);
        console.log(this.movies);
    }
}

Now, let's consider the view (using controllerAs syntax):


<div>
{{ctrl.movies}}
</div>

Upon opening the page, the initial display is {}, then when data from the websocket arrives correctly, it binds to this.movies but nothing changes. It seems like the two-way binding is not working properly. Can anyone provide insight into why this might be happening?

Answer №1

The issue does not lie with ES6, but rather stems from the fact that Angular's change detection mechanism is not aware of your socket and the need to run a digest loop.

For more information, refer to this tutorial. In the tutorial, Socket.io is encapsulated within a service that manually invokes $apply on each socket event.

app.factory('socket', function ($rootScope) {
  var socket = io.connect();
  return {
    on: function (eventName, callback) {
      socket.on(eventName, function () {  
        var args = arguments;
        //NB!
        $rootScope.$apply(function () {
          callback.apply(socket, args);
        });
      });
    }
  };
});

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

Preventing the Sending of Origin Header in Angular 2

I am facing an issue in my Angular2 project where the HTTP service is automatically adding the 'Origin' header with a value to all of the requests. Is there a way to stop Angular2 from including this 'Origin' header in the HTTP calls? A ...

The Next.js API endpoint is struggling to process cross-domain POST requests

Dealing with a POST request in my NextJS application has been challenging. This particular post request is originating from a different domain. To address this issue, I included a receptor.ts file in the /pages/api directory: import { NextApiRequest, Next ...

Optimal utilization of JSON in JavaScript API: Enhancing Performance, Reinforcing Maintainability, and Optimizing Resources

Currently, I am working on developing an application using Laravel and VueJS (along with Vuex). Although I do not have much experience in working with these frameworks or front-ends, I am curious to know the best practices for utilizing the data received f ...

Utilize Protractor to extract the text within a specified span element

Is it possible to check if the text of the span within the button is "Vigente" using expect and Jasmine? If so, how can I achieve this? <button _ngcontent-hke-28="" class="btn btn-success disabled" tabindex="-1"> <span _ngcontent-hke-28="" class= ...

Why is the last move of the previous player not displayed in this game of tic tac toe?

Why does the last move of the previous player not show up in this tic-tac-toe game? When it's the final turn, the square remains empty with neither an "O" nor an "X". What could be causing this issue? Here is the code snippet: The HTML code: <div ...

Adjust hover effects based on true conditions

Currently working on a web app using HTML, CSS, JavaScript, and AngularJS. Progress so far includes a clickable box that triggers a javascript function to display more boxes upon click using ng-click. <div ng-click="!(clickEnabled)||myFunction(app)" cl ...

"Troubleshooting: Why isn't my jQuery AJAX POST request successfully sending data to

Here is the jQuery code snippet that I am currently working with: $("#dropbin").droppable( { accept: '#dragme', hoverClass: "drag-enter", drop: function(event) { var noteid = "<?=isset($_POST['noteid']) ? ...

Bringing an AMD module into a Mocha test

I recently encountered an error while using Mocha to test code that was exported as an AMD module. When running the Mocha test, I received the following error message: ReferenceError: define is not defined at Object.<anonymous> (/home/malintha/proje ...

Converting a file from a URL to a blob in PHP for use in JavaScript

Attempting to convert an image from a URL to a blob file that can be utilized in JavaScript, but encountering challenges. Is this achievable and if so, how? Current attempts include: // $request->location is the url to the file in this case an ima ...

The function Slice() does not function properly when used with two-dimensional arrays

I have been attempting to duplicate a 2D array by value using the slice() method in order to prevent any changes made to the new array from impacting the original. Oddly enough, it seems that this approach is effective with a 1-dimensional array but not wi ...

Different ways to verify if a Checkbox is selected within a table

As a newcomer to reactjs, I have a component that renders two tables with different data using the same component and passing data through props. Each table has checkboxes for selection. If a user selects items from both tables, I want to detect if they ha ...

When trying to arrange the intelligent table, it triggers a fresh ajax request

I am currently using an Angular.js smart table that retrieves data from an Ajax call using $resource. I have implemented st-pipe to specify the ajax function and I'm utilizing st-safe-src. Upon initialization, the smart table successfully completes t ...

Assistance with JSONP (Without the use of jQuery)

I've been putting in a lot of effort trying to understand how to make a JSONP request, but all the reference materials I find are full of jQuery examples. I can go through the jQuery source code, but I prefer a straightforward and simple example. I&ap ...

Tips for consolidating outputs from three different APIs using JavaScript and AJAX? [Pseudo code example]

For my school project, I am working on an e-commerce aggregator site where I need to combine product data from 3 different APIs (like Aliexpress and Amazon) into one homepage. Although I can retrieve results from each API individually, I'm facing chal ...

Troubleshooting a Messaging Error between Background and Another Script

Attempting to transfer selected text from the current page to an HTML page using message passing: content script to background script, then background script to the HTML page. However, encountering errors if the HTML page is not already open, and even gett ...

Show a pop-up notification when the mouse passes over a word in the text

I've been grappling with this issue for days now and could really use some guidance. Despite scouring the web, I'm unsure if I've approached it correctly. What I'm trying to achieve is having an alert box pop up each time a user hovers ...

how to execute a javascript function in an ionic controller from the template

My journey in creating my first Ionic app has been smooth so far. I am not well-versed in UI design, so I may not be implementing the best practices, but I am making progress nonetheless... Within a template, I have encountered the following code snippet ...

Issue with VueJS 2 and TypeScript: computed value unable to recognize property specified in data object

When creating the following component: <template lang="html"> <div> <p>{{ bar }}</p> </div> </template> <script lang="ts"> import Vue from 'vue'; export const FooBar = Vue.ex ...

Link to the Vue Bootstrap Toast Message

I have integrated vue-bootstrap-toasts (demo) into my website to display Toasts. However, I am facing an issue with adding a link to the toast that should redirect to another page. this.$toast.success('Test is created', { href: "www.googl ...

Unpredicted organization when combining react, mapbox-gl, custom layer, and three.js

I am currently working on utilizing mapbox gl to showcase gltf buildings through a custom layer. Additionally, I am displaying the default 3d-buildings provided by mapbox in this manner: export default { id: '3d-buildings', source: 'c ...