The AngularJS controller is triggered twice

I'm facing an issue where a controller seems to be executing twice, even though I cannot find any duplicates in my files. Running a case sensitive search through my code only reveals one instance of the controller in route configuration and the actual controller file.

Route config:

$routeProvider.when('/cb159e1ec61d0613f10ab397d8bd6782',{
    templateUrl: view+'passwordreset.html',
    controller:'passwordreset'
});

Controller:

App.controller('passwordreset',['$scope','$http',
    function($scope,$http){
        $scope.token='asdf'
        console.log($scope.token); // <-- logs 2x to console
}]);

passwordreset.html:

<div class="row">
    <div class="small-12 columns">
        <fieldset class="content-box">
            password reset {{token}}
        </fieldset>
    </div>
</div>

In addition, I have tried creating a new route but the issue persists. I've also cleared my cache. Interestingly, some controllers execute once while others execute twice when I put an alert in them.

Here's another example:

//route config
    $routeProvider.when('/', {
     templateUrl: view+'home',
     controller:'Home'
    });

//template   
        <div ng-controller="testing">a</div>
//controller
Lmu.controller('Home',function($scope){
    $scope.home='home';
    alert($scope.home); <-- alerts 2x (two scopes for Home controller are created)
});

Answer №1

The issue has been identified and resolved. Upon closer inspection, it was discovered that there were two different layouts based on the user's authorization status, determined by ng-show="user.authorized" and ng-show="!user.authorized". In both cases, a <div ng-view> was present, causing the processing of ng-view to occur regardless of the condition set by ng-show.

It was later realized that everything within ng-show is processed regardless of its truth value.

To rectify this, the solution involved replacing ng-show with ng-switch, which ensured that the view would only be processed once the specified condition was met.

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

Utilizing Jest and nest.js for testing with absolute paths

Looking at my jest configuration inside the package.json: "jest": { "moduleFileExtensions": [ "js", "json", "ts" ], "moduleDirectories":["node_modules", "src" ...

Introducing React JSX via a nifty bookmarklet

Looking to convert code found in an HTML file into a bookmarklet? Here's the code snippets involved: <script src="JSXTransformer-0.13.3.js"></script> <script src="react-0.13.3.js"></script> <script type="text/jsx;harmony=tr ...

Creating dynamic links within HTML through real-time updating

In my application, I have a feature that generates a list of words and converts them into clickable links. When the user clicks on a link, I want to extract the {{word.name}} from the HTML link without navigating to a new page. I simply need to retrieve th ...

Inquiring about the rationale behind using `jquery.ui.all.css` specifically

I'm curious about the impact of jquery.ui.all.css on Tabs As soon as I remove it, <link rel="stylesheet" href="jquery/dev/themes/base/jquery.ui.all.css"> My tabs stop functioning. Here's my query: What exactly does the jquery.ui.all.cs ...

Creating a sticky menu in a column using affix with a CSS bootstrap list-group

My goal is to develop a sticky menu utilizing CSS Bootstrap affix and the list-group menu. I have successfully implemented most of it, except for one issue when the user scrolls down. Upon scrolling down, the menu expands to fill the entire width of the ...

Master the Art of Animating Letters in the DOM by Navigating Through Any Array of Characters

I am attempting to implement a typewriter effect animation where a new message is displayed and animated as I type into an input box. Initially, I tried using a global char variable to iterate through each element of the array. However, whenever I passed ...

Having issues with 'direction' in React withStyles causing errors

I am experiencing an issue with my React website where I am using the withStyles feature to apply styles to a material-ui Grid element. Specifically, when attempting to use direction: "column" in the style, I encounter the error message provided below. Th ...

Parsing JSON data as files are being read in an asynchronous manner

I have a task of reading multiple JSON files and consolidating their data into a single array. Here is my approach: const files = ['file0.json', 'file1.json', 'file2.json', 'file3.json'] To achieve this, I utilize ...

Different methods for testing AngularJS directives

Currently, I am developing a Rails 3.2 application that will utilize AngularJS. While I have successfully implemented the desired functionality using AngularJS, I am facing challenges when it comes to testing my code. To run Jasmine specs, I am utilizing g ...

"Utilizing a dynamic global variable in Node.js, based on the variable present in

I'm looking to achieve the following: var userVar={} userVar[user]=["Values"]; function1(user){ //calculations with userVar[user] } function2(user){ //calcula ...

PhantomJS version 2.1.1 encountered an error on a Windows 7 system, displaying "ReferenceError: Map variable not found."

I've been utilizing the "MVC ASP.NET Core with Angular" template. I'm attempting to incorporate phantomJS and execute the tests, but encountering the following errors: ERROR in [at-loader] ..\\node_modules\zone.js\dist&bs ...

Tips for extracting values from a Multiple Search Selection dropdown box using react-semantic-ui

I am having trouble retrieving the selected values from a Multiple Search Selection dropdown using the semantic-ui-React component. Although I have successfully rendered the items and enabled searching and selecting, I am struggling to extract the chosen v ...

Issues with displaying calendar items on the React timeline are only resolved after pressing F12 or resizing the

I am encountering a problem while trying to plot the items array in the timeline calendar. Interestingly, the groups array is working fine. The items array also functions properly when values are provided manually. However, the items only render after pre ...

The data remains stagnant even after employing the onDataChange function in react native following the integration of a reusable component

My reusable Text input component is not working properly for validation. I am unable to retrieve the input value as it always shows null. This is how I am retrieving the username and password: <LoginTextBox placeholderName='Email& ...

Fire css animations only upon the addition of a specific class

By clicking on button 2, you will witness the animation effect in action. But, if another function alters the button, like checking a checkbox to show or hide it, the animation triggers again as if the button were clicked once more. How can I prevent the ...

The addClass() and removeClass() functions in Jquery are not functioning correctly

$('.push').each(function(){ if($(':first-child',this).hasClass( "activex" )){ $(this).off().off('click').on('click',function(){ var a = $(this).attr('id'); $.ajax({ type: "POST", ...

Using the moment library in Angular to convert date and time can sometimes lead to errors

Whenever I attempt to convert a Gregorian date to a Persian date, the minute value in the conversion ends up becoming an error. For instance, let's say I want to convert this specific date and time to a Persian date: 2020-09-14T16:51:00+04:30 should ...

Encountering a Next.js installation error due to the inability to locate the module fs

Having trouble with the installation of a new Next.js 14 app. I've searched on Google and Stack Overflow but haven't been able to find a solution. I'm stuck at this point. Can anyone offer some assistance? What I have attempted: npx creat ...

What causes images to expand automatically when selected in my JavaScript gallery?

I am struggling with a simple JS gallery of images where I want to display a large image at the top and small thumbnails below it. The issue I am facing is that when I hover over an image in the thumbnail section, the big image changes as expected. However ...

Explore the functionality of the Enter key and search button with JavaScript

I have the following code that works perfectly when I click on the search button. However, I would like to know how I can also initiate a search using the enter key. Here is the code snippet: <script> function mySearch() { var text = document.g ...