Modifying the sidebar navigation in Ionic for a user who is logged in

Is it possible to modify the side menu content based on whether a user is logged in or not?

Example 1 - user not logged in:

If a user isn't logged in, this side menu will be displayed.

https://i.stack.imgur.com/iY427.png

Example 2 - user is logged in:

When a user is logged in, additional menu items are shown. These extra items are visible only for logged-in users.

https://i.stack.imgur.com/cRhp8.png

In my controller:

$http.get('http://127.0.0.1:8080/elodieService/consommateurs/'+$localStorage.idconsommateur, { params: { "idconsommateur":$localStorage.idconsommateur, fields: "nom,prenom",format:"json"} }).then(function(result) {

                console.log(JSON.stringify(result.data));
                $scope.prenomconsommateurConnect=result.data.prenom;

In the view :

 <ion-header-bar class="bar-stable" >
                <h1 class="title" ng-hide="!prenomconsommateurConnect" ng-controller="accueilController">Bonjour Hello {{prenomconsommateurConnect}}</h1>
                <h1 class="title" ng-hide="prenomconsommateurConnect" ng-controller="accueilController">Bonjour Hello link</h1>

                </ion-header-bar>

However, I always see the result as "bonjour hello link". How can I fix this issue?

What approach should I take? Should I consider using ng-if, ng-show, or ng-hide? Or perhaps there is a better solution for this scenario?

Any assistance would be greatly appreciated.

Answer №1

If you're wondering how to toggle visibility in AngularJS, you can either use ng-if or ng-show and ng-hide. Personally, I prefer using ng-if!

Here's an example implementation in a menu controller:

.controller('AppCtrl', function($scope, $ionicModal, $timeout,$ionicSideMenuDelegate,$http) {

$http.get('http://127.0.0.1:8080/elodieService/consommateurs/'+$localStorage.idconsommateur, {
  params: { "idconsommateur":$localStorage.idconsommateur, fields: "nom,prenom",format:"json"} })
  .then(function(result) {
  console.log(JSON.stringify(result.data));
    if(result.data.prenom) {
      $scope.prenomconsommateurConnect = result.data.prenom;
    }else{
      $scope.prenomconsommateurConnect = "";
    }
});

$scope.$watch(function () {
  return $ionicSideMenuDelegate.getOpenRatio();
}, function (value) {
  console.log("value " + value);
  $scope.getMenuProfile();
});

$scope.getMenuProfile = function () {
  if($scope.prenomconsommateurConnect === "" ){
    $scope.isLogin = false ;
  }else{
    $scope.isLogin = true ;
  }
};
}

And in the menu.html:

<ion-header-bar class="bar-stable">
  <h1 ng-if="!isLogin" class="title">Please Login</h1>
  <h1 ng-if="isLogin"  class="title">You are Logged In</h1>
</ion-header-bar>

I hope this explanation helps!

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

Generate an image instantly from text using ajax when a key is pressed

Currently, I am immersed in a stencil project where my goal is to convert text into an image. In this particular task, there's a textbox that captures the user input on key up event. Once the user enters text, my aim is to display that text as an imag ...

Is it possible to customize the close icons on the autocomplete feature in Material UI?

Is there a solution to change the icon while keeping its function when clicked? I am looking to replace this Icon <Autocomplete multiple id="checkboxes-tags-demo" options={top100Films} disableCloseOnSelect getOpt ...

Using AngularJS to effectively receive and handle chunked data for displaying real-time status updates

Can someone assist me with utilizing angularjs for a specific task? I am dealing with a situation where the rest server is providing several instances of status records representing a long-running process. The data comes back in chunks and I need to displ ...

Can you explain the concept of a framework operating "on top of" node.js in a way that would be easy for a beginner to understand?

If someone is new to JavaScript, how would you explain the concept of "on top of node.js" in simple programming language? I am looking for a general explanation as well as specific reference to Express on top of node.js in the MEAN stack. Appreciate your ...

AngularJS initiates an XMLHttpRequest (XHR) request before each routeChange, without being dependent on the controller being used

I'm currently embarking on a new project, and for the initial phase, I want to verify if the user has an active session with the server by sending an XHR HEAD request to /api/me. My objective is to implement the following syntax $rootScope.$on("$rou ...

I am looking to convert the input format of a timepicker using moment.js to display as 12:38:07 instead of 2018-01-23T12:38:07.439Z

In my project, I am utilizing AngularJS Material for the template and AngularJS for JavaScript. Due to the absence of a timepicker in Angular Material JS, I have opted to use a timepicker provided by Moment.js. The backend of my application is built using ...

Encountering an issue with the removal of slides when updating JSON data for angular-flexslider

Issue: Although my JSON object is updating, the slider does not update for all resorts as expected. Sometimes it fails to update when the JSON object changes. The resorts (image collections) that do not update are throwing an error stating "cannot read pr ...

Different approaches for expanding object.prototype while utilizing jQuery

Once upon a time, I made the mistake of trying to extend Object.prototype and ended up encountering errors in my jQuery file. After doing some research, I found out that extending Object.prototype is frowned upon in the JavaScript community due to potentia ...

Scrolling to an element is not possible when it has both position absolute and overflow-x hidden properties

My issue involves animating ng-view with a slideup animation, requiring absolute positioning of elements and overflow-x: hidden to clip the content. However, I need to use the scrollTo element functionality on one sub-page, which doesn't work when bot ...

What is the best way to load a partial in Rails asynchronously with AJAX?

I am currently using the following code to load a partial when I reach the bottom of a div containing a table: $(document).ready(function () { $("#pastGigs").scroll(function () { if (isScrollBottom()) { $('#pastGig ...

Is there an issue with the functioning of Angular routing?

I've been working on setting up routing in AngularJS and it seems like the server is running smoothly. However, I'm facing an issue with the angular routing not functioning correctly; only the main.html page loads in ng-view, while second.html do ...

Choosing between Angular 2 and NodeJs/Express for a massive project

Can Angular 2 and NodeJs/Express be effectively used together in a web project, or do you have to choose only one of them? ...

Encountering difficulties in transmitting ngModel across multiple layers

I'm encountering a problem when trying to pass ngModel through the second child component. It seems that as soon as I do that, it stops working. In order to pass the ngModel, I implemented this abstract class: export abstract class AbstractValueAcces ...

What is the best way to convert exponential values to decimals when parsing JSON data?

var value = '{"total":2.47E-7}' var result = JSON.parse(value); Looking to convert an exponential value into decimal using JavaScript - any suggestions? ...

Monitoring and refining console.error and console.log output in CloudWatch

I've encountered a situation where my lambda function includes both console.error and console.log statements, which Node.js interprets as stderr and stdout outputs respectively. However, upon viewing the logs in CloudWatch, I noticed that they appear ...

Limit the frequency of rendering JSX elements in React by implementing debouncing techniques

Is there a way to delay the rendering of a jsx element in order to smooth out the animation? I have an expanding panel with content inside, and when the panel is empty, the animation works well. However, if the panel already contains content, the expansion ...

Guide to activating form elements using a JQuery function

I need help setting up a form with 11 rows and 11 columns. By default, the form fields should be disabled, but when I click on an "EDIT" button, they should become enabled. Any assistance would be greatly appreciated. Thank you in advance. Here is my edit ...

What causes the Error: ENOENT open error to occur when attempting to render a doT.js view?

My first experience with doT.js led me to create a basic server setup: 'use strict'; var express = require('express'); var app = express(); var doT = require('express-dot'); var pub = __dirname+'/public'; var vws ...

Transferring sizable JavaScript array to the Web API

I've been grappling with this problem for two days... In my JavaScript code, I have a large array comprising 20,000 rows and 41 columns. This data was initially fetched in JavaScript via an ajax call, as shown below: var dataArray = []; var dataRequ ...

JavaScript's ability to call object properties at multiple levels allows for complex data

My approach involves using mongodb in conjunction with ajax calls for data retrieval. However, I have encountered an issue where the properties needed to generate HTML from JavaScript objects are sometimes missing. Consider this ajax call: $.ajax({ ...