Failure to refresh view after controller attribute triggers event

After creating a search bar that triggers an event upon completion of the search, I set up a listener in the form of a controller to handle the data passed from the search bar. This data is then bound to the view using {{ controller.node.name }}.

app.controller('Controller', function($rootScope){
  this.node = undefined;

  $rootScope.$on('searchEvent', function(event, info) {
    $timeout(function(){
      $rootScope.$apply(function(){
        set(info);
      });
    }, 0);
  });

  function set(c) {
    this.node = c;
  }
});

I came across solutions involving $scope.$apply, but they primarily use $scope.node instead of my approach using controller.attribute.

When I tried using $scope.node, it worked perfectly fine. So my question is, why isn't it working with a controller attribute?

Answer №1

The value of this is actually determined by how a function is called.

When the function set is called, this takes on a different value, resulting in the creation of a new node variable instead of editing the same one.

By using a variable instead of this, you ensure that you are referencing the same variable consistently.

app.controller('Controller', function($rootScope){
    var self = this;
    self.node = undefined;

    function set(c) {
        self.node = c;
    }
});

For more information on this, visit the link provided.

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

Custom JavaScript code in Bootstrap Navbar Toggler results in closing the menu when a dropdown is clicked

As a newcomer to JavaScript, I am currently learning the language while working on a website. I have customized a template that I found online but am struggling with the JS code that I'm not very familiar with. Here's my issue: When using the na ...

Tippy.js tooltips are not adapting to CSS styling as expected

My experience with tippy.js in my browser had been smooth until this morning. All of a sudden, they stopped responding to any CSS styling and simply defaulted to a dark grey background with border radius. However, they still respond to changes in the scrip ...

Customizing the styling of elements rendered using ng-bind-html in AngularJS

Need some help with setting styles for a div in my AngularJS code. I can successfully set the class, but struggling with setting the style directly. Even using the style tag doesn't seem to work. Here is what I have attempted: $scope.myHTML='< ...

React component that renders conditionally based on the response of an API request

I am working on a status icon that changes based on the number of errors in an object. What is the most effective method for passing data from ComponentDidMount to the function where I want to analyze it? I am trying to aggregate the status in an object, ...

Tips for resolving the issue of infinite re-render with react-hook-form?

Struggling to build a basic form in React with react-hook-form library. Implemented various validations and features in the form. However, encountering the following console error. Error message on console: Warning: Maximum update depth exceeded. This can ...

Tips for determining if a mobile web browser is active on a mobile device's foreground or background

I'm currently developing an angularjs html5 application. Just a quick question - is it possible to utilize pause/resume events in a mobile web application? Alternatively, you can help me by answering my question about how to check if the browser wind ...

Wordpress AJAX delivering empty response instead of intended string

I have tried everything, including adding die() at the end of my PHP function, but my ajax call is still returning zero. I went through other questions here but could not find a solution, so please review my code. This is the function I use to make an aja ...

Problem with parsing JSON in a mixed array

When making a YouTube API call, the response includes a var result = JSON.stringify(response, '', 2); that has the following structure: { "kind": "youtube#searchListResponse", "pageInfo": { "totalResults": 1000000, ...

Insert a new <tr> element into a dynamic table using PHP and jQuery without the need to refresh the page

I am attempting to dynamically insert a row into an existing table when a button is clicked. The rows in the table are created dynamically based on data retrieved from a PHP script. My approach involves making an ajax call to the insert_tr.php script, whi ...

Having trouble displaying the panel on Chrome, but it functions perfectly on Firefox

While attempting to display a panel in extJS 3.4.0, I encountered an issue. The panel renders correctly in Firefox but throws an error in Chrome: Uncaught TypeError: Cannot call method 'applyStyles' of null Ext.Panel.Ext.extend.createElement ...

How to delete the last item of an array in AngularJS using scope

In my Angular controller, I have an array and a method for deleting objects. function($scope, $http){ $scope.arrayOfObjects = []; $scope.remove = function(obj){ var i = $scope.arrayOfObjects.indexOf(obj); if( i > -1 ){ ...

CSRF Forbidden Error: incorrect csrfToken code: 'INVALIDCSRFTOKEN' found in Node.js

While working on my blog project, I encountered an issue with CSRF on certain routes. The CSRF protection works fine on routes like login, register, logout, and updating account information. However, when attempting to create or update a post or category i ...

The issue with AngularJS 2-way-binding failing to refresh

Recently, I experimented with angularJS in conjunction with a range-slider-directive that can be found here: https://github.com/supertorio/angular-rangeslider-directive Initially, everything worked well while using the data-model solely within my HTML pa ...

Adding Tooltips to Your Bootstrap 5 Floating Labels Form: A Step-by-Step Guide

Can you include tooltips or popovers in Bootstrap 5 floating labels text? I've set up a form with floating form fields and I'd like to provide instructions for certain fields using tooltips or popovers. I can make it work with the standard form ...

"Uncovering the problem: Challenges with AngularJS videos

Currently, I am diving into the world of AngularJS by exploring the content provided on the website . I have downloaded the videos and am diligently working through the examples on my personal computer. Everything was progressing smoothly until I encounte ...

Make sure to clear the output field before clicking the submit button, or after submitting any further information

When I submit values from an input field to my Flask/Python back-end using this form, the API GET will return a value if the ticker ID is found. However, if it's not found, nothing happens, and the last price remains there, which can be misleading as ...

Error encountered in Ionic app: array.push() is not a valid function

A situation arose in my application where I have a controller and factory set up. Inside the factory, there is an array that should hold IDs of certain elements. However, when attempting to push an element into the array, an error is triggered stating: ...

The height of the window in Chrome on android is not being properly reset when the address bar

It appears that Chrome on Android is not resetting the window height when the address bar is hiding, unlike other browsers. I have a function that returns true when you have scrolled close to the bottom of the page. function isBottom() { return docume ...

What is the best way to update the div id by extracting the last digits from a number?

Is there a way to change the div ids using a function? Before: <div id="a_1">a1</div> <div id="b_1">b1</div> <div id="c_1">c1</div> <div id="d_1">d1</div> <button onclick="change()">Change</button& ...

Adjust the color of the output result in real-time

Having trouble changing the color of a JavaScript result based on whether it's positive or negative. I've tried several solutions but can't seem to find one that works. Here is my latest attempt: JavaScript: var calc = (a + b) * c; doc ...