Displaying data fields according to the information stored in $localStorage

Is it possible to display specific fields on a page depending on the value stored in $localStorage?

For example, consider the following code snippet:

<div ng-controller="myCtrl">
    <ul>
        <li> List 1 </li>
        <li> List 2 </li>
        <li> List 3 </li>
        <li> List 4 </li>
    </ul>
</div>

If I only want to show List 3 and List 4 when the value of $localStorage is not equal to 0, how can I achieve this?

Answer №1

To retrieve a value from the $localStorage and assign it to a scope variable, you can use the following code snippet and then check the variable using ng-if directive.

$scope.condvalue = localStorage.getItem("yourItem");

HTML:

<div ng-controller="myCtrl">
    <ul>
        <li> List 1 </li>
        <li> List 2 </li>
        <li ng-if="condvalue!=0"> List 3 </li>
        <li ng-if="condvalue!=0">  List 4 </li>
    </ul>
</div>

Answer №2

First, make sure to include $window in your controller in order to utilize localStorage.

// Define the variable ListisnotZero here
$window.localStorage.setItem("ListisnotZero", ListisnotZero);

// Retrieve it from the local storage
$scope.ListisnotZero = $window.localStorage.getItem("ListisnotZero");

// In the HTML template
<div ng-controller="myCtrl">
    <ul>
        <li>List 1</li>
        <li>List 2</li>
        <li ng-if="ListisnotZero != 0">List 3</li>
        <li ng-if="ListisnotZero != 0">List 4</li>
    </ul>
</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

Achieving Style= Background-Img URL Extraction with Cheerio

I am attempting to retrieve the URL of the background image. The background image is located within an a href tag. The a href tag contains the following style: style="background-img:url("")" I am using cheerio (a Node.js module simila ...

PHP displaying incorrect value after modifying value in JavaScript

One issue I am facing is with an html page that submits a form through javascript. Prior to the submission of the form, I modify the value of a hidden tag, which should then be retrievable in php. However, when attempting to retrieve the value in php, it a ...

Is there a way to translate this PHP $_POST statement into ColdFusion?

Looking to stop spammers with the web form obfuscation method detailed here. The challenge is that my forms are in ColdFusion on ColdFusion servers. if( !isset($_POST['name'])) { die("No Direct Access"); } // Ensure form submission $name = $_ ...

I'm experiencing some difficulties utilizing the return value from a function in Typescript

I am looking for a way to iterate through an array to check if a node has child nodes and whether it is compatible with the user's role. My initial idea was to use "for (let entry of someArray)" to access each node value in the array. However, the "s ...

Authentication failed due to Bcrypt.compare() returning invalid credentials

const express = require('express'); const router = express.Router(); const auth = require('../../middleware/auth'); const bcrypt = require('bcryptjs'); const jwt = require('jsonwebtoken'); const config = require(&apo ...

Is there a more efficient method for creating HTML with coffeescript / jQuery besides using strings?

Today marks my first attempt at creating a "answer your own question" post, so I hope I am on the right track. The burning question in my mind was, "Is there a more efficient way to generate HTML with jQuery rather than using tag strings?" My goal is to c ...

What are the steps to access an Alexa skill through a web browser?

I recently developed an Alexa skill for recipe recommendations. I am wondering if it is possible to have the skill open a browser on my phone and display the recipe when I say, "Alexa, send me the recipe"? The skill is working perfectly in the Alexa devel ...

Retrieve JSON data from an external website

I am looking to display the number of players currently playing on a different poker website on my own site. The necessary data is provided in JSON format by this link (tournaments.summary.players). I have tried using a .getJSON request, but it seems like ...

Utilizing jQuery to Perform Calculations with Objects

Can someone help me with a calculation issue? I need to calculate the number of adults based on a set price. The problem I'm facing is that when I change the selection in one of the dropdown menus, the calculation doesn't update and continues to ...

Find the nearest element with a specific class using jQuery

I am currently using jQuery version 1.12.4 for the purpose of retrieving the value from the closest element with a specific class selector. Unfortunately, I am encountering difficulty in selecting the closest element as desired. $(function() { $("[cla ...

What is the best way to transfer the value of a <span> element to a different <div> using jQuery or JavaScript

Is there a way to move or copy the price and insert it into the <div class="info"> using jQuery? The code I'm currently using is returning an unexpected result of "102030". jQuery(document).ready(function($) { $(document).ready ...

The function .then is not compatible with AngularJS

I have a service that is responsible for loading data. angular.module('App').service('daysService', ['$http','$q',function($http,$q) { var days = []; return { loadDay: function() { ...

What is the process for generating a submatch for this specific expression?

Trying to extract account status information using a regular expression in the DOM. Here is the specific string from the page: <h3>Status</h3><p>Completed</p> Current regular expression being used: <h3>Status</h3>[&bs ...

The Vue v-on:click event listener seems to be unresponsive when applied to a

I've been attempting to utilize the on-click directive within a component, but for some reason, it doesn't seem to be functioning. Whenever I click on the component, nothing happens, even though I should see 'test clicked' in the consol ...

Tips on creating a parameterized SQL query within Javascript

Does anyone know how to properly write a parameterized SQL Query in javascript? I attempted it myself, but encountered an error. I even tried another method, yet I'm still facing syntax errors. let sql =select * from q_users where firstname=?,[${na ...

Error encountered in jQuery's addClass and removeClass functions: Unable to read the property 'length' of an undefined value

Upon loading my page, I aim to have some of the div elements hidden initially and display only one. Here is a script that accomplishes this goal: <script> $(document).ready(function () { $(".total").click(function () { $("#pi ...

Incorporate Aria Label into a Website Link

Currently working on enhancing website accessibility. I have identified a close menu button that lacks an Aria Label, and my goal is to rectify this using JavaScript. Although I am utilizing the script below to target the specific ID and add the attribute ...

"Effortless integration of JavaScript with PHP for streamlined database access

My current project involves working with a database table containing two fields - one for URLs and the other for descriptive text. These fields will be updated regularly by a separate script. The task at hand is to create a timer that checks the database ...

Node.JS Plugin Management - Enhance Your Application with Customized

Currently, I am deeply involved in the development of a complex application using Node.JS, built on top of Express. I decided to implement a flexible plugin system to make things easily plug-and-play. The structure of this system consists of: root/ | p ...

Creating stylish error labels using Materialize CSS

While Materialize has built-in support for validating input fields like email, I am looking to implement real-time validation for password inputs as well. This would involve dynamically adding error or success labels using JavaScript. Unfortunately, my at ...