search

Home  >  Q&A  >  body text

angular.js - angular页面传值问题

请问 angular-ui-route 中的

$state.go('xxx',{id:'1'})

只能传递这种简单字符串吗?
我想传object应该怎么传 用全局变量吗?

巴扎黑巴扎黑2744 days ago621

reply all(4)I'll reply

  • phpcn_u1582

    phpcn_u15822017-05-15 16:54:32

    Not only can you pass simple strings, but of course you can also pass objects

    1. The name of the parameter in the router configuration. Take ui-router as an example

      $stateProvider.state('state1', {
          url: '/path/:id', // 这个地方用简单字符串
          templateUrl: '/path/to.html',
          params: {
              obj: null // 这个地方就可以随便你用了. 因为这个参数没在state的url中体现出来
          }
      }).
      
    2. Use $state for page switching

      $state.go('state1', {
          id: '22',
          obj: {
              key: 'value'
          }
      });
      
    3. Use $stateParams in controller to get parameters

      console.log($stateParams.obj)
      

    Of course, if you pass parameters that are not reflected in the URL, the parameters will not be available during operations such as going back.

    You can use @whosesmile's method to operate. Use service. LZ can take a look at angular's built-in $cacheFactory. 使用这个可以生成一个缓存, 执行put get remove removeAll and other operations

    reply
    0
  • 巴扎黑

    巴扎黑2017-05-15 16:54:32

    http://stackoverflow.com/a/25999849/2586541

    reply
    0
  • 大家讲道理

    大家讲道理2017-05-15 16:54:32

    Usually two options
    1. Global variables, that is, using $rootScope
    2. Use service. Because service is a single instance and will not be destroyed, you can define a general cache data service to access data. Call service.set(key, value) in the controller that needs to transfer data. When needed Call service.get('key')

    in the controller that read the previous data

    reply
    0
  • 伊谢尔伦

    伊谢尔伦2017-05-15 16:54:32

    I forgot which version ui-router added the following method to maintain the parameters in the url
    1.$state

     $state.go("state",{datas:{ID:data1,NAME:data2}})
    

    2.Configure router

    .state('state',{
    url:'path/{datas:json}',
    params:{'datas':null}.
    ...
    })
    

    3. Use $stateParam.datas to get

    reply
    0
  • Cancelreply