Unusual phenomena exhibited by my variable scopes

I am a beginner in this industry and I have a desire to create an app. However, I am facing some challenges at the moment. Below is a simplified version of my code. My goal is to have the API display only the signUp option initially. Upon clicking on signUp and then pressing submit, I want the button to switch to display only signout.

Currently, my scope variable is not functioning as intended and both buttons are being displayed. Can you please make the necessary modifications and explain the reason behind this issue? If possible, kindly utilize rootscope, scope, and sc.logIn() so that I can learn how to implement it correctly. Thank you!

https://plnkr.co/edit/ewhLZsKKTWTzlECsj4xO?p=preview

angular.module('myApp',['myApp.dashboard','myApp.signUp']);

angular.module('myApp.dashboard').controller('mainControl',mainControl);
mainControl.$inject = ['$rootScope','$scope'];
function mainControl($rootScope,$scope){
  $rootScope.logged=false;
    $scope.logged=$rootScope.logged;
}


angular.module('myApp.signUp').controller('signUpControl',signUpControl);
signUpControl.$inject = ['$rootScope','$scope'];
function signUpControl($rootScope){
    alert("haha");
    this.logIn=function(){
        $rootScope.logged=true;
    };
}

Answer №1

There are a few key issues present in your code that need to be addressed.

When declaring a module without specifying any dependencies, the module will attempt to fetch an already declared or defined module. To create a new module, it is important to pass in the necessary dependencies or provide an empty array if there are none.

Additionally, it is crucial to monitor any changes made to the scope to ensure that these modifications are properly communicated to the controllers and reflected in the view.

Here is a functional solution that addresses these issues:

angular.module('myApp', ['myApp.dashboard', 'myApp.signUp']);

angular.module('myApp.dashboard', []).controller('mainControl', mainControl);

function mainControl($rootScope, $scope) {
  $rootScope.logged = false;
  $rootScope.$watch(() => $rootScope.logged, function() {
    $scope.logged = $rootScope.logged;
  });
}

mainControl.$inject = ['$rootScope', '$scope'];

angular.module('myApp.signUp', []).controller('signUpControl', signUpControl);
signUpControl.$inject = ['$rootScope'];

function signUpControl($rootScope) {
  this.logIn = function() {
    var a = 1;
    $rootScope.logged = true;
  };
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.8/angular.min.js"></script>
<div ng-app="myApp">
  <div ng-controller="mainControl as mc">
    <div>{{mc.logged}}
      <div ng-hide="logged">

        <button type="button" class="btn" data-toggle="modal" data-target=".signUp">Sign Up</button>
      </div>
      <div ng-show="logged">
        <button>Sign Out</button>
      </div>
    </div>


    <div class="signUp modal fade" id="sighUp" ng-controller='signUpControl as sc'>
      <div class="modal-dialog">
        <div class="modal-content">
          <div class="modal-header">
            <h4 class="modal-title">sigh up</h4>
            <button type="button" class="close" data-dismiss="modal" aria-label="Close">&times;</button>
          </div>
          <div class="modal-body">
            <form id="signUpForm" name="signUpForm" ng-submit="sc.logIn()">
              <label for="email">
                <span>email:</span>
                <input type="email" name="email">
              </label>
              <button class="submit" id="signUpSubmit" type="submit" value="signUp">submit</button>
            </form>
          </div>
          <div class="modal-footer">

          </div>
        </div>
      </div>
    </div>
  </div>
</div>

An issue seems to persist with submitting the form using the provided code snippet.

You may explore a working solution here: Codepen

Answer №2

There's nothing strange about it.
Check your console for error messages.
The issue seems to be with the angular bootstrap error.

angular.module('myApp.dashboard', [])
-------------------------------   ^^  ------- should be an array, even if empty 

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

With the power of Jquery, conceal a div that has not yet materialized but will come into existence

Is it possible to use JQuery to hide a div when another div is clicked, even if the div to be hidden is only created upon clicking? Consider the following code example: HTML: <div class="first">Click here to create the second div</div& ...

Tips for efficiently displaying a computed property on a template using Vuetify?

I am attempting to display only the initials of a user who is logged in while inside a store. Here is my template: <v-menu v-if="this.$store.getters.getLoggedUser"> <template v-slot:activator="{ on, attrs, userInitials }&quo ...

Tips for adding borders to specific rows and columns using ALASQL in Excel and AngularJS

Is it possible to add borders to specific columns and rows in my code? I've been trying but haven't found an option for it yet. columns: [ {columnid:'email'}, {columnid:'dob', title: &a ...

Transfer the text entered in one textbox to another textbox located within a dynamic table

Is there a way to automatically copy the input from a textbox into another textbox within a dynamic table using JavaScript? I need the value entered in the first textbox to be replicated in the textbox inside the dynamic table. Any guidance or assistance o ...

What is the best way to incorporate a JSON file into a JavaScript class?

I attempted to load a JSON file within a JavaScript class, but encountered an issue where the loaded data was only accessible inside a specific function. class CreatePage { constructor() { var request = new XMLHttpRequest(); request. ...

Using the AngularJS double Array ng-repeat feature allows for easy iteration

I am in the process of developing a web application using AngularJS. My goal is to: Display both sets of arrays in one list using ng-repeat and eliminate any null values. For example, Value One A, Value One B, Value Two A, Value Two B Issues I am fa ...

What could be causing my variable to not be stored correctly by my useState hook?

In my current setup, I have a basic form displayed within a modal window. The purpose of this form is to update my redux store with the text entered into the textbox field. I am attempting to utilize the useState hook in order to properly set the value of ...

Using ng-repeat with ng-model; accessing the value of ng-model

I find myself in a peculiar situation - Behold the Controller snippet - $scope.filterNum = {0: 'filterVal1', 1: 'filterVal2', 2: 'filterVal3', 3: 'filterVal4', 4: 'filterVal5', 5: 'filterVal6', ...

Why does routing function correctly in a browser with AngularUI Router and Ionic, but not in Ionic View?

My Ionic App runs smoothly in the browser when using ionic serve. However, I encounter issues with routing when running the app in Ionic View (view.ionic.io) after uploading it with ionic upload. The index.html loads but nothing within <div ui-view=""& ...

Tips for updating the minimum value to the current page when the button is pressed on the input range

When I press the next button on the current page, I want to update the value in a certain way. https://i.stack.imgur.com/XEiMa.jpg I have written the following code but it is not working as expected: el.delegate(".nextbtn", "click", f ...

How to use React MUI Checkbox to set the checked value as false

I'm encountering an issue with the React MUI - Checkbox component. On my site, I have a checkbox linked to a handler that targets a REST API endpoint. This endpoint sets a boolean flag for the user in the database on a POST request and returns the boo ...

Exploring the process of querying and modifying subdocuments in MongoDB

As I work on updating a subdocument in MongoDB with Express, I have a specific route set up to find the exact object for updating. Currently, I am only able to replace the entire document with a new one. Is it acceptable to keep it like this or is there a ...

Implementing specifications throughout the entire nodejs route

In my Nodejs app, I have a RESTful API where I need to check for a user's role before sending a response with data or 404 error. apiRouter.route('/users') .get(function (req, res) { var currentUser = req.decoded; if(curr ...

A helpful guide on how to dynamically input database values into the href attribute of an 'a' tag

How do I successfully pass an ID to href in my code? When I try to return it via req.params, it keeps coming back as undefined. I need the ID from the URL in order to use the findOne method to access the data stored in the database. I've searched thr ...

Adding a Byte to a Hexadecimal Escape Sequence in JavaScript: A Step-by-Step Guide

I am currently facing an issue with JavaScript and due to my limited expertise in this area, I am seeking assistance. The challenge at hand involves hashing a "string" of bytes, where I need to add another byte that is generated within a script. Adding th ...

What is the best method for assigning a user input value from HTML to a JSON variable?

After trying various methods, I am still struggling to make it work. My goal is to send a discord webhook, but I keep encountering a post error. I'm not sure if the issue lies with my JSON variable or something else in my code. Here is the snippet of ...

Sending an Ajax request to C# code

UPDATE After some research, I discovered what seems to be the "correct way" to handle the issue by using a combination of JSON.stringify and creating a model, as explained in this thread. I still can't figure out why the original approach didn't ...

The paragraph tag remains unchanged

I am currently working on developing a feature that saves high scores using local storage. The project I'm working on is a quiz application which displays the top 5 scores at the end, regardless of whether the quiz was completed or not. However, I&apo ...

Expanding the original state of the parent by incorporating additional perspectives

Utilizing: AngularJS and UI-Router. I am currently working on a webpage that consists of two views: menu and main. The menu view remains constant, while the main view changes based on the content. To avoid defining both views in every state, I have create ...

Guide to implementing a Bootstrap dropdown in an Angular application

I am implementing a bootstrap dropdown with checkboxes and looking to integrate it into Angular. http://codepen.io/bseth99/pen/fboKH (I have made modifications like wrapping the dropdown in a form) <form> <div class="container" ng-controller ...