简体   繁体   English

Angular js $ state.go,参数不起作用

[英]Angular js $state.go with parameter not working

I have tried to implement angular js $state.go() with parameter. 我试图用参数实现angular js $ state.go()。 But the $state.go() works fine without parameter. 但$ state.go()在没有参数的情况下工作正常。 But with parameter it didn't works. 但是使用参数它不起作用。 I already tried many examples but no way. 我已经尝试了很多例子,但没办法。 I need to display the parameter in html view. 我需要在html视图中显示参数。 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是

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

what is the problem.? 问题是什么。?

You cannot use parameters directly in the HTML. 您不能直接在HTML中使用参数。 You first have to add it to the scope (or virtual model if you use that), eg 首先,您必须将其添加到范围(或虚拟模型,如果您使用它),例如

$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 而且你必须在你要去的州的控制器中这样做,而不是那些你称之为$ state.go的州。

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. 谢谢大家。

I guess you can't use objects in params. 我想你不能在params中使用对象。 Just replace 只需更换

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

to this: 对此:

params: {
      value:''
}

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM