How to verify if an object is empty in an AngularJS expression

I need to display either a Login or Logout button based on the value of a $rootScope variable. Currently, only the Logout button is showing up in the li tag below. I have specific actions that should occur after certain events:

After Logging In:-

$rootScope.userData = response.userdata; //response.userdata is an Object

After Logging Out:-

delete $rootScope.userData;
$rootScope.userData = null;

In the Controller:-

console.log($rootScope.userData); //Object {}

Html Code:-

<li ng-if="$root.userData == null"><a href="#/login"><i class="fa fa-lock"></i> Login</a></li>
<li ng-if="$root.userData != null"><a href="#/logout"><i class="fa fa-lock"></i> Logout</a></li>

Answer №1

login ng-if="!$root.userData.length"

logout ng-if="$root.userData.length"

I suggest creating a MainController, loading loggedin data into a model named user, and adding this MainController to the <body> tag for easy access to the loggedin user throughout the application instead of adding it to rootScope.

You could also develop a service that determines if a user is logged in and returns data. By creating a model called isAuthenticated with either true or false based on the login status checked by the service, you can use ng-if="isAuthenticated" and ng-if="!isAuthenticated".

Answer №2

Consider utilizing ng-show instead of ng-if in your code.

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

Is it possible to merge JavaScript files exclusively using encore?

I am working on a Symfony project with a Twitter Bootstrap template where the assets are hardcoded in Twig. I would like to use Encore to manage assets, but I want it to only combine JavaScript files without compiling them further. Is there a way to confi ...

Tips for dynamically incorporating input forms within AngularJS

I am trying to dynamically change the form inputs using ng-bind-html. However, I am only able to display the label and not the text box on the DOM. The content inside ctrl.content will depend on the values received from the server. Important Note: The ...

Structuring React components - Incorporating a form within a modal

I am currently utilizing the react-bootstrap Modal, Form, and Button components. My goal is to have the button trigger the modal window containing a form. Once the form is filled out, clicking another button within the modal will validate the data and sen ...

Combining AddClass and RemoveClass Functions in Mootools Event Handlers

I am currently in the process of creating a CSS animation, and one aspect involves changing the class name of the body at specific intervals. Since I am relatively new to Mootools (and JavaScript in general), my approach has been to add/remove classes to ...

Error caused by live data dynamic update on Highstock chart: Cannot access property 'info' of undefined

Utilizing Laravel, I was able to create Highcharts by consuming data from a Rest API link (Spring app) at . The data is currently displayed statically, but I want to dynamically visualize it on charts. I attempted to follow this example on Fiddle and inte ...

Encountered an issue while attempting to retrieve the access token from Azure using JavaScript, as the response data could

Seeking an Access token for my registered application on Azure, I decided to write some code to interact with the REST API. Here is the code snippet: <html> <head> <title>Test</title> <script src="https://ajax.google ...

Updating data in AngularJS after inserting a new record

What is the most efficient method to update comments data when a new record is added to the database? I currently have this code that works well, but I am concerned that it may be slow if there are a large number of comments. Any assistance would be greatl ...

Checking for duplicate entries in an array created with the Angular form builder

I am currently utilizing angular6 reactive form with form builder and form array. The issue I am encountering is duplicate subject entries from the drop down in the form array. How can I implement validation to prevent duplicate entries in the form array? ...

Is there a way to define the route path when using MemoryRouter in Jest without using location or history objects?

Apologies if this question has already been asked before. I have a query regarding MemoryRouter in React. I am able to set initialEntries and initialIndex to customize "location" and "history", but the "match" parameter does not update as expected. It is ...

Converting PHP date format to JavaScript date format

I'm struggling to solve this problem $datetime = new Date().toLocaleString(); // returns current date in format 10/21/2021, 14:29:43 How can I generate the desired date format using JavaScript? The output should look like this: 2021-10-21 16:30:01 ...

Ways to trigger a function when the body is loaded

In this snippet, I am attempting to execute the oauth2_login() function when the body loads, which is intended to log in the user. Currently, I have hardcoded values from the database into the username and password fields. <!DOCTYPE html> <html&g ...

Avoid triggering the resizecolumn event in ExtJS while the columns are still loading

Currently, I am involved in a project using ExtJS 6.2 and facing a challenge related to performing operations when the columns in a grid are resized. It seems like the suitable event for this task is columnresize. However, the issue arises because the colu ...

Preventing Component Duplication in Vue.js: Tips to Avoid Displaying Two Instances on the Screen

After developing a Vue app in VS Code, I encountered an issue where the home component was rendered twice on the screen when attempting to run the application. Below is a screenshot of the resulting homepage: Here is the code from Home.vue: ...

Accessing files from the local system using AngularJS

During my time working with RequireJS and Backbone, I utilized requirejs/text as well as requirejs-plugins to load local json files that were typically used for configuration purposes. With AngularJS, how can one achieve the same functionality? Many sugg ...

Can you explain the distinction between Array() and [] in Javascript, and when would it be preferable to use one over the other?

Similar Question: Understanding the difference between "new Array()" and "[]" in JavaScript array declaration When working with JavaScript, you have the option to create a new array using: var arr = new Array(); or simply using: var arr2 = []; Wha ...

My content is being obstructed by a single-page navigation system

I attempted to create a simplified version of the issue I am facing. Basically, I am working on a header with navigation that stays at the top of the page while scrolling. The problem arises when clicking on a section in the navigation. The screen scrolls ...

Exploring jQuery AJAX Attributes during ajaxStart and ajaxStop event handlers

With my project consisting of over 40 ajax webservice calls, I am looking to incorporate additional debugging features. One such feature is a timing method which I have already developed using my Timer class/object in Javascript. I'm seeking assistan ...

Updating a database with a loop of React Material UI toggle switches

I am trying to implement a material UI switch feature that can update the Active and De-Active status of users in the database directly from the Admin Panel. Currently, the database updates are functioning correctly when toggling the switches. However, th ...

How does Socket.IO manage the functions that are provided to it?

This project involves the use of angularJs, nodeJs, and mongoDb. On the client side, I have the following code: webSocket.emit('createNode', node, function(node){ /* ^ */ /* ...

Hybrid App Date Selection Tool: Modern Calendar Picker for Cordova and Phonegap

Perhaps my question is repetitive, but I am searching for a way to display a native calendar style datepicker in Cordova/Phonegap apps on both Android and iOS. Despite numerous attempts, I have not been successful in finding a solution. I did come across a ...