Passing no data in Angular.js ui-router.Angular.js ui-router

Within my Ionic application, there is a need to pass parameter(s) from one sub-view to another. Initially, the parameters are successfully passed as expected. However, upon returning to the first sub-view and then navigating to another sub-view, the parameters seem to get lost. Here is the relevant code:

index.html (project/www)

<!DOCTYPE html>
<html>
    <head>
       <link href="lib/ionic/css/ionic.css" rel="stylesheet">
       <script src="lib/ionic/js/ionic.bundle.js"></script>
       <script src="lib/ngCordova/dist/ng-cordova.js"></script> 
       <script src="cordova.js"></script>
       <script src="js/app.js"></script>
       <script src="js/main.js"></script>
       <script src="js/routes.js"></script>
   </head>
    <body ng-app="app" animation="slide-left-right-ios7" >
        <!-- main window -->
        <div>
            <ion-nav-view></ion-nav-view>
        </div>
    </body>

</html>

route.js (www/js/route.js)

angular.module('app.routes', [])
  .config(function($stateProvider, $urlRouterProvider) {
     $stateProvider
        .state('mainUser', {   // initial view
           url: '/mainuser',
           templateUrl: 'templates/mainUser.html',
           controller: 'mainCtrl'
        })
       .state('userDrive', {  // destination view
          url: '/user_drive',
          params: {
             driveData: null  // passing parameter
          },
          templateUrl: 'templates/user_drive.html',
          controller: 'DriveCtrl'
    });
    $urlRouterProvider.otherwise('/mainUser');
 });

templates/mainUser.html

 <ion-side-menus>
    <ion-side-menu-content> 
        <ion-content class="padding" style="background-color:#fff;">        
            <input type="date" ng-model="selectData.selectedDate" placeholder="Date">

            <input type="time" ng-model="selectData.selectedTime" placeholder="Time">
         </ion-content>
         <div class="bar bar-footer">
             <div class="button-bar">
                 <a ui-sref="userDrive({ driveData: selectData })" class="button"> Drive </a> 
            </div>
         </div> 
     </ion-side-menu-content>
  </ion-side-menus>

Upon clicking the Drive button, the user gets redirected to the userDrive sub-view with the driveData parameter intact. However, when I navigate back to mainUser, make changes in the input fields, and try clicking on Drive again, the redirection to userDrive happens without the driveData parameter being passed.

userDrive Controller

.controller('DriveCtrl', function($scope, $state, $stateParams) {       
     console.log("DriveCtrl");  // after redirecting to this controller for the second time
                                // it seems that no values are passed
                                // possibly indicating that DriveCtrl is not being called
     console.log("$stateParams :  ", $stateParams);       
})

Answer №1

An issue arises when Ionic caches the view, causing the controller to only run once and resulting in the log being visible just on the initial load.

To resolve this, utilize Ionic's view events by referring to the documentation here: http://ionicframework.com/docs/api/directive/ionView/

Answer №2

The solution provided by @leandro-zubrezki is correct. To clear the cache, simply insert the following code snippet in the 'DriveCtrl' controller:

$scope.$on("$ionicView.afterLeave", function () {
    $ionicHistory.clearCache();
});

Answer №3

It is not possible to pass an object as a state param; each value must be sent separately.

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

What causes the discrepancy in results between the quoted printable encoding operation in JavaScript and Oracle?

Programming Languages: // JavaScript code snippet //https://www.npmjs.com/package/utf8 //https://github.com/mathiasbynens/quoted-printable par_comment_qoted = quotedPrintable.encode(utf8.encode('test ąčęė')); console.log('par_comment_qot ...

NextJs's React-Quill is unable to effectively highlight syntax using the highlightJS library

I have been working on a NextJs application (blog) that utilizes react-quill as a rich text-editor. As part of my setup, I am making use of the Next custom 'app' feature, where my UserProvider component wraps everything to provide global access t ...

Retrieving input values from forms

angular.module('app', []) .controller('mainCtrl', ['$scope', function ($scope) { $scope.checkVal = function () { console.log('entered'); console.log($scope.data.user) } ...

What is the default method to activate textAngular automatically?

When using textAngular, it remains inactive when the page first loads and only becomes active once the user clicks on the input field. This means that all buttons, such as "Insert video," will be disabled until the text input is focused. Is there a method ...

Prevent the left border from displaying on a link that wraps to a new line

Excuse me, I am facing an issue with a pipe separator in my subnav bar between links. The problem is that the first link on a new line retains the left-border that should be disabled. How can I prevent this border from appearing on the first link of a new ...

Obtaining HTML data with ajax within a WordPress post

Greetings! I've been putting together a website and I'm eager to include a unique timeline in each of my posts. I'm utilizing WordPress for this project. However, since the timeline I want to insert will vary from post to post, I am unable t ...

Clone a specific link within a div using jQuery only one time

I have a group of divs and I want to copy the link from the first div and put it into the last div (mobile-link). Currently, it is either duplicating all the links and inserting them all at once, or if I use :eq(0), it's placing the first link in each ...

What is the strategy to load a div exclusively when triggered by a click event, instead of loading it

Can anyone assist me with a problem I am facing on my scripting page? I am currently working on a website that showcases properties. I would like to know how to prevent a specific div from loading when the page initially loads, and instead have its content ...

How can I replace specific text with HTML code in a webpage?

In my application, users have the ability to write comments that may contain HTML code. However, this code is escaped before being displayed: <div class="card-body"> <p class="card-text"> &lt;h1&gt;HOLA&lt;/h1&gt; Cita:#2&am ...

What is the process for monitoring a property in Angular?

Seeking clarification on Angular - is it possible to detect property value changes within the same class? import { Component } from '@angular/core'; @Component({ selector: 'my-app', templateUrl: './app.component.html', ...

I am facing difficulty in loading my services in AngularJS

I am reaching out for assistance with a recurring issue I have encountered while working with AngularJS. Throughout my coding education, the greatest challenge for me has always been linking files together. However, in AngularJS, this task has become parti ...

Struggling with the jquery .hover method

Encountering an issue with the jquery .hover function. It seems to only work if I place it inside a generic $(function(){. I have observed it being done without this generic function, so if anyone can point out what I might be doing incorrectly, I would be ...

The Module Design Pattern in Jquery

In my project, there is a jQuery module pattern that I am having trouble understanding its purpose. There is also a custom plugin called jquery.skInit.js (function($) { $.fn.skInit = function() { return this.each(function(i,element) { var e = ...

Steps for adding an onclick function to a collection of div elements

My current project involves utilizing the Flickr API and I am interested in creating a popup div that opens when clicking on each image. The goal is to display the image in a larger form, similar to how it's done using Fancybox. I'm looking for ...

Tips for Iterating through Nested Arrays using the Inside Array in a Dynamic Way

I am facing an issue with my code as it lacks flexibility when a new array is added to the nested array, in which case the new array is not considered. My main concern is how to access the elements of each nested array simultaneously. Here's an examp ...

Troubleshooting Recursive Logic Problem with hasOwnProperty in JavaScript and JSON

JSON data with a specific problem highlighted by the comment // doesn't get parsed currently: var oarsObject = [{ "coordinateReferenceSystem": "26782,15851 <-- not in a value", "positionReferenceType": "geogWgs84", "geogWgs84": ...

How can you check the boolean value of a checkbox using jQuery?

I have a checkbox on my webpage. <input id="new-consultation-open" type="checkbox" /> My goal is to store the state of this checkbox in a variable as a boolean value. consultation.save({ open: $("#new-consultation-open").val() }); Unfortunate ...

Invoke the HTTP API from the device application

I need to make an API call to a server that does not have the HTTPS protocol. I came across this post on Stack Overflow: https://stackoverflow.com/questions/57433362/api-calls-made-in-ionic-4-app-not-working-on-android-device The post mentions that HTTP ...

Dealing with Sideways Overflow Using slideDown() and slideUp()

Attempting to use slideUp() and slideDown() for an animated reveal of page elements, I encountered difficulty when dealing with a relatively positioned icon placed outside the element. During these animations, overflow is set to hidden, resulting in my ico ...

Looking for a specific search term within the middle of strings in an array

Is there a way to improve the autocomplete feature of my input field so that it shows suggestions based on any part of the word typed in? I currently have it set up to only show suggestions if the input matches the start of a word in the array. How can I m ...