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

Manually incorporating multiple React components into a single container

I am currently in the process of upgrading an old JavaScript application that relies heavily on jQuery by introducing some React components. The existing code utilizes JS/jQuery to dynamically add elements to the DOM, and I am looking for a way to replace ...

The button's background color will vanish if you click anywhere outside the button or on the body of

Struggling with a tabpane created using HTML, CSS, and JavaScript. The problem arises when the background color of the active tab's button disappears upon clicking outside the button or on the body. While switching between tabs, I can change the color ...

Example: Utilizing data transfer from model to directive

I have a question regarding a specific part in an example from Thinkster. I believe my confusion is due to my limited understanding of JavaScript and AngularJS fundamentals. I've been teaching myself since December, starting with the basics of JavaScr ...

Utilizing X-editable and Parsley to trigger dual Ajax calls

Here is a snippet of code I am working with: $.fn.editable.defaults.mode = 'inline'; var editable = $('.x-editable').editable({ send: 'always', validate: function() { var form = editable.next().find('form ...

Anticipate that the function will remain inactive when the screen width is less than 480 pixels

Implementing Functionality in Responsive Navigation <script> document.addEventListener('DOMContentLoaded', () => { let windowWidth = window.Width; let withinMobile = 480; let close = document.getElementById('close&ap ...

PHP - Implementing a Submit Button and Modal Pop-up AJAX across multiple browsers in PHP

As a newbie in PHP AJAX coding, I'm facing a challenge with having two browsers. My goal is to click the submit button on one browser and have a modal popup on the other browser simultaneously. Essentially creating a chat system where only a button an ...

Vue-Router functions only on specific routes

While my vue-router correctly routes the URL for all menu buttons, it's not displaying each Vue component properly. You can see a demonstration here. Here is a snippet of my HTML (using Vuefy) <div class="navbar-start"> <a ...

JSON object containing elements with dash (-) character in their names

While I am in the process of parsing a `json` object, I encountered an element labeled as `data-config`. Here's an example: var video = data.element.data-config; Every time I attempt to parse this specific element, an error pops up: ReferenceError ...

Utilize JavaScript, MySQL, and PHP to input data into a database

My JS function is supposed to make an ajax request, but for some reason it's not working. I've checked the URL in an alert and all variables are declared. var request = new XMLHttpRequest(); var url = "ajax_js/q_ajax.php?q="+ques+ ...

What is the correct method to authenticate a user's ID token in Firestore with Javascript?

I am in the process of developing a react native application and have integrated Firebase, specifically firestore, for data management purposes. My current goal is to incorporate an automatic login feature in my app, where users remain signed in even after ...

The MUI persistent drawer navigation bar becomes dysfunctional when accessing a specific route

Exploring the MUI library for the first time, I successfully created a navigation bar that functions properly for one route (Dashboard). However, when attempting to implement it on the candidate route, it collapses as shown in this Screengrab of collapsed ...

Incorporating a new function into a TypeScript (JavaScript) class method

Is it possible to add a method to a method within a class? class MyClass { public foo(text: string): string { return text + ' FOO!' } // Looking for a way to dynamically add the method `bar` to `foo`. } const obj = new MyCl ...

What methods can a controller use to verify the legitimacy of the scope?

I'm a bit perplexed when it comes to validation in angular. It seems like all of the validation is connected to the form. But what happens when the controller needs to ascertain if the model is valid or not? Here's an example I quickly whipped u ...

unable to display images from a folder using v-for in Vue.js

Just getting started with Vuejs and I have two pictures stored on my website. The v-for loop is correctly populating the information inside databaseListItem. The path is /opt/lampp/htdocs/products_db/stored_images/cms.png https://i.stack.imgur.com/969U7.p ...

Parcel Bundler works perfectly fine on localhost, however, an error is being displayed on the index.html file in the Dist folder

Currently, I am in the process of learning how to use Parcel for bundling purposes. I have set up a index.html file that is connected with index.js. Surprisingly, everything works perfectly fine when I access it via localhost:1234 using Parcel. However, wh ...

Creating a variable in JavaScript

In my HTML body, I have set up a global variable named index with the value <%var index = 0;%>. This allows me to access it throughout the code. I'm trying to assign the value of $(this).val() to <% index %>, but I can't seem to get ...

Retrieve a JSON object from a Knockout observable array using the object's ID

I'm struggling to find examples of ko.observablearrays that deal with complex JSON objects instead of simple strings. I have an observable array containing a large JSON object with several properties, and I need to retrieve a specific object based on ...

I'm encountering an issue with my array in JavaScript while using // @ts-check in VS Code. Why am I receiving an error stating that property 'find' does not exist on my array? (typescript 2.7

** update console.log(Array.isArray(primaryNumberFemales)); // true and I export it with: export { primaryNumberFemales, }; ** end update I possess an array (which is indeed a type of object) that is structured in the following manner: const primar ...

ngInfiniteScroll Activates on Every Scroll Occurrence

Implementing ngInfiniteScroll for endless scrolling on my website has required me to set the height of the outer div to a specific value. Without this adjustment, the Infinite Scroll feature activates unintentionally. <div style="height: 1px"> &l ...

Encountering the "potential null object" TypeScript issue when utilizing template ref data in Vue

Currently, I am trying to make modifications to the CSS rules of an <h1> element with a reference ref="header". However, I have encountered a TypeScript error that is preventing me from doing so. const header = ref<HTMLElement | null> ...