Controller fails to initialize when utilizing $location.path or $state.go

Issue with Controller Initialization after Redirect in Ionic App

I have encountered an issue where the controller is not initializing on the same page when I use $state.go or $location.href. In my Ionic app, I am using a sidemenu to pass category Id to the product page using a service. However, the data is not updating correspondingly to the current category Id. For example, when I click on 'aaa', I am successfully redirected to the product page and it alerts 1. But when I click on 'bbb' from the side menu, I get no alert. Interestingly, if I choose 'bbb' first, then it alerts 2 and vice versa.

Sidemenu Template

<ion-item  nav-clear menu-close ng-click="allproductpage(1)">aaa</ion-item>
<ion-item  nav-clear menu-close ng-click="allproductpage(2)">bbb</ion-item>

Sidemenu Controller

$scope.allproductpage= function(a){
angular.extend(inpsf.inps, {act_cat : a})  // inpsf is service
$location.path('/app/allproducts')
}

Product Page

.controller('AllproductsCtrl', function($scope,inpsf)
{
alert(inpsf.inps.act_cat)   
})

P.S I have included

$ionicConfigProvider.views.maxCache(0);
in my .config file

Answer №1

For proper handling of parameters in the state path declaration, make sure to listen for $routeChangeSuccess if utilizing $location, or $stateChangeSuccess if using $state. Since you are already on the '/app/allproducts' when clicking the second menu item and the controller is already active, consider implementing code like this:

.controller('AllproductsCtrl', function($scope,inpsf)
{
    $scope.$on('$routeChangeSuccess', function() {
        alert(inpsf.inps.act_cat)  
    })
})


//where you configure your states..
$stateProvider.state('/app/allproducts/:cat', {

Side menu controller

$scope.allproductpage= function(a){
    angular.extend(inpsf.inps, {act_cat : a})  // inpsf is service
    $location.path('/app/allproducts').search({act_cat: a})
}

Answer №2

Consider implementing the following method.

$ionicHistory.clearCache().then(function(){ $state.go('newroute') })

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

Changing a global variable via an AJAX call

I seem to be facing a common issue that many others have encountered. Despite my understanding that global variables can be modified inside functions in Javascript, I am struggling with this concept in practice. var lastMessage = 0; function loadChat() { ...

Issues with simple jquery and javascript: unable to add the class ".has-error" and onclick event not properly defined

Currently, I am working with jQuery version 2.1.4 and JavaScript to implement a straightforward form validation using Bootstrap. During this process, I have encountered three peculiar issues. While I am confident that my jQuery code is properly functi ...

Show a message popup or view based on the validation of the model

Picture an online store with multiple steps to complete during the checkout process. Whenever the customer clicks the 'next' button, it triggers the corresponding 'Action' method. This method includes a validation check to ensure that t ...

Having trouble launching simple ionic template: Issue with locating module 'fast-deep-equal'

Recently I started using Ionic and followed the steps to install the basic blank template as shown below. First, I installed Ionic and Cordova by running the command: npm install -g ionic Cordova. After that, I created my first Ionic project using the f ...

Utilize a function on specific attribute values

I have a function in JavaScript that is designed to convert all relative URLs into absolute URLs. function rel2Abs(_relPath, _base); //_relPath represents the relative path //_base indicates the base URL Now, my objective is to implement this function on ...

Shut down a pop-up overlay

I've been facing a challenge in implementing greasemonkey to automatically close a modal when the "x" button is clicked. Allow me to share with you the code snippet for the webpage: <div class="modal-header"> <button type="button" class="clo ...

Remembering data in a lazy loaded AngularJs list

Encountering an issue with a lazy loaded list of books. Initially, 10 books are loaded and if the user scrolls down, the next 10 books are loaded. For example, if the user is on "page" 4 and clicks on the 35th book to view details, upon returning to the l ...

Error in Node-Fetch Mapping: Unable to access property 'map' of an undefined entity

Encountering an issue with the "map" section when attempting to run it - receiving an error message stating "Cannot read property 'map' of undefined" The customers constant is defined above, so I'm unsure where the undefined value is origin ...

What causes the list to be empty at first when using the "!" (not) in an AngularJS filter?

<p><input type="text" ng-model="test"></p> <ul> <li ng-repeat="x in names | filter:'!'+test"> {{ x }} </li> </ul> Is there a way to first show all items in the list and then progressively excl ...

I am encountering difficulties when trying to integrate ng-grid into my application

On the webpage <meta name="viewport" content="width=device-width" /> <title>Welcome to Husys</title> <link href="~/CSS/layout.css" rel="stylesheet" /> <link href="~/CSS/style.css" rel="stylesheet" /> <script src="~/Scrip ...

Exclusive pair of vertices within a network

I am working with a diagram that includes nodes A, B, C and several edges connecting these nodes. How can I extract the distinct pairs (A, B), (A, C), (B, C)? One potential method is: visited = []; for item1 in nodes: for item2 in nodes: if (item ...

AngularJS Tip: Ensuring addClass is not called on directive during page load

I am facing an issue with a directive that triggers a CSS animation on page load instead of waiting for a button click event. I need the animation to only start when a button is clicked. How can I stop the animation from running automatically? JSBIN Dire ...

Are you looking for a way to prevent the default behavior of an event in angular-ui-router 1.0.x? Check out

Recently, I made a change in my code where I replaced $stateChangeStart with $transitions.onStart $rootScope.$on('$stateChangeStart', function(e, ...){ e.preventDefault(); // other code goes here... }); Now, the updated code looks lik ...

Is there a way to position two Grid elements to the left and one to the right in Material UI, especially if the first Grid element has a specified width

I'm currently using Material UI and have a Grid layout with three items. The left item needs to have a fixed width of 240px and be aligned to the left. The middle item should be left justified and can have any width, containing buttons that I've ...

I could really use some assistance with this script I'm working on that involves using ($

Using Ajax for Form Submission: $.ajax({ url: 'process.php', type: 'post', data: 'loginName=' + $("#loginName").val() + 'loginPass=' + $("#loginPass").val(), dataType: 'json', success: func ...

Mocking objects in unit test cases to simulate real-life scenarios and test the

How do I pass a mocked event object and ensure it is validated? onCallFunction() { const eventValue = event; if (!eventValue.relatedTarget || !eventValue.relatedTarget.last.contain('value')) { super.onCallFuncti ...

Troubleshooting: AngularJS filter is not functioning properly in combination with jQuery

I have a collection of items stored in the variable $scope. I have connected these items to jQuery events and added an input field for filtering. Everything is functioning properly. However, when I enter text into the input field, the filtered out items r ...

Tips for triggering animation only when the element is in the viewport

I'm currently developing in React and facing a challenge where I need to trigger a fade animation for an element only when it becomes visible on the screen. The issue is that right now, the animation plays as soon as the page loads, which defeats the ...

Have you ever noticed how Angular-cron-jobs consistently produces identical cron expressions for tasks such as "Every Minute," "Every Hour at 0 past hour," "Every Day at 0:0," and so on?

If you experiment with DEMO BASIC on , you will notice that "Every Minute", "Every Hour at 0 past hour", "Every Day at 0:0", and "Every Week on at 0:0" all result in the same expression: *****. A similar issue arises when selecting Every Year, as regardle ...

Angular with PHP Integration Success

I'm navigating the realms of Angular and PHP, understanding that Angular is client-side while PHP operates on the server. I'm curious if there's a way to merge these two technologies, as it's all quite unfamiliar territory for me. For ...