2

I have tried to implement angular js $state.go() with parameter. But the $state.go() works fine without parameter. But with parameter it didn't works. I already tried many examples but no way. I need to display the parameter in html view. my state provider is,

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

and controller is,

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

and my div is

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

what is the problem.?

2
  • You're showing the home state and trying to go to dash_home which is a completely different state, not even a child state of home Commented Jun 1, 2016 at 11:06
  • home is my current state and dash_home is the state which i want to redirect Commented Jun 1, 2016 at 11:19

3 Answers 3

2

You cannot use parameters directly in the HTML. You first have to add it to the scope (or virtual model if you use that), e.g.

$scope.obj = $state.current.params.obj

And you have to do it in the controller of the state that you are going to of course, not in those where you call $state.go

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

3 Comments

would you please give me an example.?
I am not quite sure what do you mean. You redirect to the 'dash_home' state. So if this state has a controller, then you just have to use the example from my answer there to map the state parameters to the scope. The problem is that it angular can't render the data since it does not know about it. You have to tell it by adding it to scope.
I don't see that you call your function at all and moreover your state.go uses the state that does not exist. Perhaps you meant just 'home' there? Otherwise you'll have to add the proper state.
2

I found my mistakes. The correct code is,

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

controller is,

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});
    }

});

thanks all.

Comments

1

I guess you can't use objects in params. Just replace

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

to this:

params: {
      value:''
}

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

2 Comments

Could you provide a fiddle?
Are you kidding? You've provided a fiddle with syntax errors and without any added scripts. Plus you're using $state.go('dash_home') but as I and @Alon Eitan wrote: $state.go('home')

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.