Having trouble with passing parameters in AngularJS $state.go?

Having some trouble implementing AngularJS $state.go() with a parameter. It seems to work fine without the parameter, but when I try to pass one in, it's not working as expected. I've looked at several examples and tried different approaches, but haven't found a solution yet. I really need to display the parameter in the HTML view. Here is my state provider configuration:

  $stateProvider
    .state('home', {
        url: '/',
        views: {
          'content@': {
            templateUrl: 'content.html',
            controller: 'dashCtrl'
          }
        },
    params: {
        obj: {
          value:''
        }
    }
      });   

And here is my controller code:

     dashboard.controller('dashCtrl', function ($scope, $http, $state, $stateParams){
               $state.go('dash_home', {obj: {value:'admin'}});
});

Additionally, here is how my div is structured in the HTML:

<div>
    <h1>welcome : {{value}} || {{obj.value}}</div>

Can anyone spot what might be causing the issue?

Answer №1

Using parameters directly in HTML is not recommended. It's best to first add them to the scope (or virtual model if preferred), like this:

$scope.data = $state.current.params.data

Make sure to do this in the controller of the relevant state, rather than in any instances where you use $state.go

Answer №2

After reviewing my work, I was able to identify and correct my mistakes. The accurate code snippet is as follows:

$stateProvider
    .state('home', {
        url: '/',
        views: {
            'content@': {
                templateUrl: 'content.html',
                controller: 'dashCtrl'
            }
        },
    params: {
        value:''
    }
});

The controller for this code should be structured as shown below:

dashboard.controller('mycontroller',function($scope, $http, $state, $stateParams){

    $scope.user=$state.params.registerData;

    $scope.redirect=function()
    {
        $state.params.registerData='mycontent';

        $state.go('dash_home', {registerData:$state.params.registerData});
    }

});

I appreciate all the assistance provided in resolving this issue.

Answer №3

It appears that using objects in params is not allowed. To resolve this, simply update the code from:

params: {
    obj: {
      value:''
    }
}

to:

params: {
      value:''
}

Additionally, change

$state.go('dash_home', {obj: {value:'admin'}});
to
$state.go('home', {value:'admin'});

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 is the reason for the user not being defined in this scenario?

I am struggling to understand why I cannot set the user in the app. Registration and login functionalities are working fine. However, after refreshing the app, the user data is lost while the access token remains intact. Additionally, when attempting to cr ...

Is it possible to determine the time format preference of the user's device in Angular? For example, whether they use a 24-hour system or a 12-hour system with AM

In Angular, is there a way to determine whether the user's time format is set to 24-hour or 12-hour system? Any help would be greatly appreciated. Thanks! ...

How can I change a time duration (e.g. 3 hours 23 minutes) to a specific date and time in

Is there a way to transform a time string such as 2h 25m 15s or 2h 10m into a datetime, or simply convert it to minutes like 1h => 60, using NodeJS? ...

Attempting to construct a webpage with the ability to create visual content

I've been struggling to kick off this project because I'm uncertain about what exactly I should be looking into. I would really appreciate any guidance on where or how to start. The concept is to enable users to upload an image of a postcard, in ...

Refresh the DATATABLE inside an AJAX call without reloading the entire page

I'm currently working with a table that utilizes the Datatable plugin. I have successfully implemented filtering and deletion functionality within the table. However, after deleting certain entries, I noticed an issue where the deleted item still app ...

Customizing response headers in vanilla Node.js

My Node.js setup involves the following flow: Client --> Node.js --> External Rest API The reverse response flow is required. To meet this requirement, I am tasked with capturing response headers from the External Rest API and appending them to Nod ...

In Visual Studio Code, beautification feature splits HTML attributes onto separate lines, improving readability and code organization. Unfortunately, the print width setting does not apply

I have done extensive research and tried numerous solutions, When using Angular and formatting HTML with Prettier, it appears quite messy as it wraps each attribute to a new line, for example: <button pButton class="btn" ...

In IE the OnFocus and OnBlur style changes are not functioning properly, while they work successfully in Fire Fox

Greetings everyone, I have defined the following styles in my CSS: .on_focus { background-color:yellow; } .off_focus { background-color:white; } Now, within my form, there is this input field: <label>Last Name</label>< ...

Having trouble with my NodeJS POST route implementation with Axios

I encountered an issue with my Post route using axios. Upon clicking the button to create a new user, I received the following error message: (node:13901) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'username' of undefined ...

Krajee Bootstrap File Input, receiving AJAX success notification

I am currently utilizing the Krajee Bootstrap File Input plugin to facilitate an upload through an AJAX call. For more information on the AJAX section of the Krajee plugin, please visit: Krajee plugin AJAX The JavaScript and PHP (CodeIgniter) code snippe ...

JSON object fails to iterate with ng-repeat

It must be the scorching temperature... Having a json object that I'm eager to loop through using ng-repeat, it should be straightforward, but alas! it's just not cooperating. Here's the HTML snippet: <a data-ng-repeat="x in template.m ...

Prevent multiple submissions by disabling the Submit button in Magento after it has been clicked and

I am looking for a solution to disable the submit button or display a loading image over the form once the submission has been validated. I have tried various methods to disable the submit button after it is clicked, but these do not account for validation ...

When encountering [Error in render: "TypeError: Cannot read property 'length' of undefined"] across various files, what could be causing it to appear in more than one instance?

Currently, I am engrossed in a Vue tutorial centered around creating a basic email application. However, during my test run, the inbox's content failed to display as expected (referencing the images attached below). The first image depicts my version, ...

Could someone provide me with a breakdown of this code utilizing the .bind() function?

The code snippet above is from the jQuery source code and it deals with event handling. It defines an array of events, such as click, focus, blur, etc., and then adds each event to the jQuery prototype using a forEach loop. var events = ['click', ...

Canvas featuring labels at the top with a strikethrough effect when utilizing a pie chart in chart.js

I am working on a piece of code that aims to showcase a summary of the most popular forms based on the number of inserted rows in their respective database tables. The goal is to visually represent this data using a pie chart generated with chart.js 2.8. & ...

Exploring the depths of design in material-ui

I've just started diving into material-ui and decided to create a simple app in the SandBox: https://codesandbox.io/s/eager-ride-cmkrc The styling using jss is a bit unusual for me, but with your help on these two exercises, I'm sure I'll ...

Merge three asynchronous tasks into a single observable stream

I have 3 different observables that I am using to filter the HTML content. Here is the TypeScript code snippet: fiscalYear$ = this.store$.select(this.tableStoreService.getFiscalYear); isLoading$ = this.store$.select(this.tableStoreService.tableSelector ...

Are cookies created during an AJAX call still applicable for future (non-AJAX) requests?

While working with AngularJS, I encountered a situation where I sent a cookie back in the response during a post on the server side. My question is: will this cookie be utilized for future requests (on the same domain/host), even for NON ajax calls? ...

Placing a small image on top of multiple images using CSS

I am facing a CSS issue and I need help with positioning a small image (using position absolute) like a warranty badge on top of larger images. The challenge is to ensure that the badge is fixed at the bottom left corner of each image, despite variations ...

Error message: The Javascript Electron app is unable to find the node-fetch module when

Below is the code snippet from my gate.html file: <html> <head> <meta http-equiv='Content-Security-Policy' content='default-src 'self'; https://example.com.tr/validkeys.txt'> <meta http-equiv=&ap ...