Activate the button when the local storage variable is present

How can I display edit and delete buttons only when local storage is not empty?

In my view.html file, I attempted the following:

<div ng-controller="MainController">     
     <button ng-disabled="localStorage.getItem('wimmtkey') !== null"  > Edit</button>
</div>

Then in the review.controller.js file, I have the following function:

function submit() {  
    if($rootScope.name!=null)    {
        var temp={
            "name":$rootScope.name,
             "surname":$rootScope.surname,
             "email":$rootScope.email
        }
        $scope.localArray.push(temp);
        localStorageService.set("wimmtkey", $scope.localArray);
        $scope.obtained_array = localStorageService.get("wimmtkey"); 

        var Results = UniversalService.PostReview(JSON.stringify(JSONObject));
        }
    }

However, the button always remains visible even when using incognito mode. What could be causing this issue?

Answer №1

The button will always be visible because ngDisabled is being used instead of ngShow, ngHide, or ngIf. When ngDisabled is true, the button is simply made unclickable.

Based on the condition, it looks like the button will be disabled if the wimmtkey item exists in localStorage. This seems to contradict what you are aiming for, based on your explanation.

Additionally, have you added localStorage to the $scope object? It's not clear from your code, and you can only utilize it in the view if it is present as $scope.localStorage. Without it, the condition will never be met (and may actually throw an error that Angular ignores), resulting in the button always remaining enabled.

Answer №2

To enhance the functionality of localStorage.wimmtkey, consider creating a wrapper:

<div ng-controller="MainController">     
    <button ng-disabled="LS_wimmtkey!==null">Edit</button>
</div>

var MainController = function($scope) {
    $scope.LS_wimmtkey = localStorage.getItem('wimmtkeys');

    $scope.$watch("LS_wimmtkey", function() {
        localStorage.setItem('wimmtkeys', $scope.LS_wimmtkey); 
    });
};

When working with the submit function, ensure you utilize the $scope property:

function submit() { 
    //insert your code here
    $scope.LS_wimmtkey = $scope.localArray;
    //insert your code here
}

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

Sharing data between two PHP pages via an AJAX request

On my webpage, specifically on page 1.php, I am saving a variable to an input field text. Name:abc Done. After clicking the 'done' button, the name abc is sent to another page called 2.php via an Ajax request. In 2.php, I am storing the value ...

Postponing a hyperlink during webpage loading

Is there a way to use Jquery to delay the appearance of a link on my webpage? I'd like for visitors to have a few seconds to view the ads before the link is displayed. ...

Troubles with displaying Google Maps on Ionic Modal

Having trouble displaying the google map in an ionic modal - it shows up fine on the page but not in the modal. Any help would be greatly appreciated, as this is quite frustrating. Below is my controller js and code for the ionic modal. $ionicModal.from ...

The ng-change function does not seem to be functioning properly within the UI-select2

Why isn't ng-change firing when I change the selection in the dropdown menu? Here is the HTML and JavaScript code: <ui-select ng-model="DemandLine.SupplierId" theme="select2" ng-change="GetSupplierInfo()"> ...

Working with promises and Async/Await in Express/node can sometimes feel ineffective

Currently, I am delving into the world of node and express with the aim to create a function that can extract data from a CSV file uploaded by the user. My challenge lies in the fact that the data is being outputted as an empty array before it goes through ...

Achieve dynamic styling and adjust window size using window.open

My mind is exhausted after days of trying: function prtDiv( o ) { var css = 'body {font:normal 10px Arial;}' ; var wo = window.open('','print','width=600,height=600,resizable=yes'); wo.document.write( '<he ...

Sending an ArrayBuffer from Angular to Electron results in the window crashing

In my Electron application, I have integrated an Angular application internally. I am trying to download a byte array (ArrayBuffer) via an API call and then passing this data to a method connected through electron.remote.require('./file-service') ...

serverless with Node.js and AWS encountering a 'TypeError' with the message 'callback is not a function'

Within my handler.js file, I am utilizing the getQuotation() function from the lalamove/index.js file by passing the string "hi" as an argument. 'use strict'; var lalamove = require('./lalamove/index.js'); module.exports.getEstimate = ...

Unable to permanently uninstall Node.js

I recently downloaded the forever package using the command: npm install forever -g However, when I attempted to uninstall it using: npm uninstall -g forever I received the message up to date in 0.042s Despite this, I am still able to access the forev ...

Transforming a form containing recurring elements into JSON

Sorry if this topic has already been discussed, I wasn't able to locate any information I currently have an html form structured like so: <form> <div> First Name <input name="FirstName" type="text"> Age <input name="Age" t ...

In what way does AngularJS asynchronously navigate through the Angular template made up of HTML and AngularJS Directives?

While diving into the AngularJS book penned by Brad Green and Shyama Sheshadari, I stumbled upon the following insightful passage: The initial startup sequence unfolds as follows: 1. A user triggers a request for your application's first page. ...

Retrieve the JavaScript object that was initialized within a function

As I venture into the world of working with javascript objects and php objects, I've encountered an issue. While everything seems to be functioning correctly so far, I'm having trouble accessing the javascript object created in the ajax success r ...

Is it feasible to set a default value in an HTML input field that is not editable using JavaScript?

Is there a way to set a default value in an input field of HTML that is not editable, and then allow users to add additional text to it? For example, having 'AB' as the default and uneditable starting characters, followed by any numbers such as A ...

Where is the best place to insert Javascript code in an HTML file - the head or body section?

I am currently developing a search engine and have implemented code to automatically redirect users from HTTP to HTTPS when they visit my site. The only question I have is whether I should place this code in the head or body section of my webpage. if(wind ...

The C# MVC Controller is having difficulty retrieving decimal or double values from an Ajax POST request

Having trouble sending decimal or double values via ajax to my C# MVC Controller. The values always come through as null, even though they work fine when sent as strings or integers. Why is this happening? When checking the client's request, the corre ...

triggering two separate functions with ng-click

I'm attempting to trigger two separate functions with the ng-click event, using ng-click="Reset();Search()". Unfortunately, it doesn't seem to be working as expected. Can anyone confirm if this is the correct approach for calling multiple functio ...

Tips for sending routeparams value to model window in AngularJS

In the current project, I am working on implementing a modal window for the edit screen functionality. The process involves displaying a list of items on the screen, from which users can select one row and then click on the modify button to open the edit s ...

Determine the sequence of a div based on its class

Here is the code snippet I am working with: <div class="test"></div> <div class="test"></div> <div class="test"></div> <input type="button" class="button" value="get number"> <div class="test"></div> & ...

Exploring AngularJS and Express with Node.js: navigating routes and refreshing pages

Everything seems to be running smoothly on my app, but the moment I hit F5 in my browser is when an issue arises. The structure of my app is as follows: Using nodeJs and express, with the following routes set up in Express: core.app.get('/', f ...

Hiding or removing DOM elements with ng-bootstrap pagination

I am in the process of incorporating pagination into my project using ng-bootstrap pagination. In my HTML, I am combining an ngFor loop with the slice pipe to filter elements for display. <tr *ngFor="let bankAccount of bankingAccounts | slice: (p ...