0

I have a form that allows to update all the fields of an element. After the update succeeded, I want to redirect to the page with the list of all elements. I want to pass this page the message "update succeeded".

In the controller I pass a parameter to the state provider in this way:

$state.go('list', { message: "Update success" }, { reload: true });

In the state provider:

    function ($stateProvider, $urlRouterProvider) {
        $stateProvider
            .state('list',
                {
                    url: '/List',
                    templateUrl: 'elementsList.html',
                    params: {
                        message:
                            function ($stateParams) {
                                return $stateParams.message;
                            }
                    }
                })

Is this the correct way to pass a string parameter to an html page? What is the syntax in the html page to show the parameter value?

Thanks.

1 Answer 1

1

I would do it this way:

function ($stateProvider, $urlRouterProvider) {
    $stateProvider
    .state('list',
     {
       url: '/List/:message',
       templateUrl: 'elementsList.html'
     }
})

an then access it in the controller like this. $stateParams.message Don't forget to inject $stateParams in your controller. You can easily assign the value to a $scope variable $scope.message = $stateParams.message and use {{message}} in your view to display it.

Note that there's also an event:

$scope.$on('$stateChangeSuccess', 
function(event, toState, toParams, fromState, fromParams){ ... })

The parameter can be accessed with toParams.message in this case.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, this way I can see the message in the controller and assign it to $scope, but {{ message }} is not filled and I don't see it in the page.
What happens if you define $scope.myData = {message: $stateParams.message} and use {{myData.message}}? Be aware that you should avoid using primitives like explained here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.