ng-hide not refreshing

I've encountered an issue while trying to change the view being displayed through a broadcast in my application. Despite updating the model in the controller, the ng-show does not reflect the new state.

To make the RegisterForm controller visible, I am using

$rootScope.$broadcast("registerFormShow", username, password)
. The ng-show directive is based on the register.active variable, however, it does not seem to update as expected. I attempted calling $scope.$apply() from within the $on callback but received an error message stating that $apply is already in progress. Can anyone shed light on why this might be happening?

Here is a snippet of the relevant code:

app.controller("LoginController", ["$http", "$rootScope", function($http, $rootScope){

    this.openRegister = function(username, password){
        $rootScope.$apply(function(){
            $rootScope.$broadcast("register", username, password);
        });
      this.loggedIn = true;
        console.log("register event sent");
    }

}]);
app.controller("RegisterController", ["$http", "$scope", function($http, $scope){
    this.active = false;
  $scope.$on("register", function(username, password){
        this.active = true;
    }
}]);

HTML:

<div ng-controller="RegisterController as register" ng-show="register.active" class="cover">
        <div class="form register-form" ng-class="{indeterminate: register.indeterminate}">

            <input type="text" name="username" required="" ng-model="username" />
            <input type="password" required="" ng-model="password" />
            <button ng-click="register.register(username, password)">Register</button>
        </div>
    </div>

For a demo showcasing this problem, visit: http://jsfiddle.net/7LLa7zot/1/

Answer №1

If you've encountered a problem, try the following solution:


this.active = false;
var _this = this;
$scope.$on("register", function(username, password){
    _this.active = true;
}

It's important to note that identifying the correct "this" in JavaScript can be tricky.

This adjustment resolved the issue with Fiddler, indicating it may be related to your code. To clarify, the problem lies in using the incorrect "this" within your register function.

Answer №2

When using the .$broadcast method, parameters should be sent as an object rather than separate arguments. See below for an example:

$rootScope.$broadcast("register", {
    'username': username,
    'password': password
});

http://jsfiddle.net/7LLa7zot/5/

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

Struggling with Conditional rendering in React JS - Need help!

I am currently working on a web application that fetches product/data from a backend API and displays it on the frontend. I am also implementing an 'add to cart' and 'remove from cart' functionality. My goal is to display 'add to c ...

Dealing with an empty req.body parameter in a NodeJS function for a Rest API using ExpressJs and sequelize

Looking for assistance with integrating ExpressJS and sequelize. I have a main function (fullaccount.js) that needs to invoke two functions ("accoun.js" and "people.js") receiving data in "fullaccount.js" for processing. However, while req.body is ready in ...

Using NodeJS, pull JSON data from a JavaScript file and render it in a route file

I am currently utilizing Nodejs Express to work with a script that generates an array of objects from the Google API. My objective is to integrate this JSON data into my templates. How can I invoke the function within my script file from my route file? Be ...

Change a single-row array to a multi-row array

I currently have an array that is returning values in the following format: 0: 1,2,3,4 However, I would like it to return array values in a different way: 0: 1 1: 2 2: 3 3: 4 If I am using JavaScript, what is the best approach to accomplish this? ...

Utilize Angular components in TypeScript to effectively manage errors within forms

I am currently developing a form in Angular/Typescript with more than 10 fields, and I want to streamline error management without redundancy in my HTML code. Here is an example of the form structure : <form [formGroup]="myForm"> <label&g ...

Differing values are found within nested ng-repeats with matching keys

Hey there! I'm encountering an issue in my code, and the image below displays the response data I've received from the API: https://i.sstatic.net/nFdCL.png This is the code snippet where I'm trying to create a nested ng-repeat for my Genre ...

Vue.js - Difficulty displaying fetched data from API using v-for

My attempt to render a list of data seems to be hitting a roadblock - the data doesn't display when the page loads. The API call works perfectly, fetching all the necessary data and setting it to my data object. Here's the code snippet: once &apo ...

Adding options to a dropdown list dynamically within a ASP.NET Datagrid using Jquery

While the function in my 'aspx' page is functioning properly, I am facing an issue with the 'dropdown' inside the GridControl. It appears to be getting duplicated from the original row and not updating with the values from the appended ...

The custom div slider speed is too quick, specifically when transitioning back from a different browser tab

While observing the code in action, everything seems to work fine but when I switch to another browser tab and come back, the speed of the animation is too fast. Below is the jQuery code snippet: var divId = 1; var lp = 0; $(document).ready(function (){ ...

How can I automatically check all checkboxes in a row when a material table is loaded and uncheck them when clicked?

I have been working on a project using React Material Table and I am trying to figure out how to make the "select all" checkbox default checked when the page is loaded. Additionally, I want the ability to deselect any checkbox if needed. I attempted to use ...

Learn how to effortlessly transfer a massive 1GB file using node and express

Encountering an issue when attempting to upload a large file to a node js instance using express, resulting in failure for files of significant size. The error message received is as follows: Error: Request aborted at IncomingMessage.<anonymous> (/s ...

Unfortunately, I am unable to utilize my Async Validator as it is wrapped within the "__zone_symbol" object

I have created an asynchronous validator for passwords. export class PasswordsValidators{ static oldPasswordMatch(control: AbstractControl) : Promise<ValidationErrors> | null { return new Promise((resolve) => { if(control. ...

Is it possible to incorporate knockout.js within a Node.js environment by utilizing .fn extensions in custom modules?

I'm currently exploring the possibility of implementing the knockout.mapping.merge library in node.js, but I seem to be facing a challenge when it comes to extending ko objects within the module's scope. I am struggling to figure out how to exten ...

Adjust your path by 45 degrees

I'm facing a challenge in getting all three of my images to fly off the screen in different 45-degree directions. Currently, they are all flying off the screen to the left, but I would prefer them to go in 45-degree angles. I've shared the CSS, H ...

Display the initial JSON data object without the need to choose a link with the help of AngularJS

I have successfully built a webpage that displays JSON data based on the selected link. However, I am facing an issue where I need to show the first JSON data object before selecting any link (initially). Check out the Plunker demo here: http://embed.plnk ...

Ways to eliminate an object from an array using Javascript or Jquery

I have two arrays of objects (with the same properties) where I need to remove all objects with the same name as those found in filesToRemove from the array files. However, when I attempt to use the code below, it throws an error: Uncaught TypeError: fil ...

Optimally displaying extensive lists on a webpage using HTML

Are there any advanced javascript libraries that can efficiently handle loading a large list by only loading the visible part and simulating the scrollbar? <div id='container'> <!-- Empty space to mimic scrollbar, but preloading conte ...

retrieve the state property from NavLink

I am encountering an issue with passing objects through components in my project. Specifically, I have a chat object within a component that defines a NavLink. When a user clicks on the ChatsElement, which is a link, the page navigates to the URL /friends/ ...

Using a function parameter to restore the values of an array

Looking to clear an array using a function parameter. Below is my implementation: <script> fruitArray = ["apple", "banana", "orange", "pineapple"] function clearFruitArray(myArr){ myArr = []; ...

The search for the item in JSON by Node JS ProductProvider.getItem is unsuccessful

As a newcomer to JavaScript, I've been scouring the web for an answer without any luck. Coming from a relational database background, I'm having trouble locating a specific record in JSON. This is the JSON snippet I'm working with: Add to ...