Angular ui-router : Passing params between states [duplicate]

回眸只為那壹抹淺笑 提交于 2019-12-12 03:25:13

问题


I'm trying to pass a message from state1 to state2 in ui-router.

looked up most of the question on here about this topic but unfortunately can't make sense of it.

This is my main code and $stateProvider :

myApp.config(function($stateProvider, $urlRouterProvider) {
  //
  // For any unmatched url, redirect to /state1
  $urlRouterProvider.otherwise("/state1");
  //
  // Now set up the states
  $stateProvider
    .state('state1', {
      url: "/state1",
      templateUrl: "state1.html"
    })
        .state('state2', {
      url: "/state2",
      templateUrl: "state2.html"
    })

});

Here is a Plunker of what i created to test this out.

What should i do to make it work? Thanks


回答1:


something like this

myApp.config(function($stateProvider, $urlRouterProvider) {
  //
  // For any unmatched url, redirect to /state1
  $urlRouterProvider.otherwise("/state1");
  //
  // Now set up the states
  $stateProvider
    .state('state1', {
      url: "/state1",
      templateUrl: "state1.html"
    })
        .state('state2', {
      url: "/state2/:id",
      templateUrl: "state2.html"
    })

});

or

 myApp.config(function($stateProvider, $urlRouterProvider) {
      //
      // For any unmatched url, redirect to /state1
      $urlRouterProvider.otherwise("/state1");
      //
      // Now set up the states
      $stateProvider
        .state('state1', {
          url: "/state1",
          templateUrl: "state1.html"
        })
            .state('state2', {
          url: "/state2",
          templateUrl: "state2.html",
          params: {id: ""}
        })

    });

then the ui-serf

<a ui-sref="state2({id:'value'})">Send param and go to page 2</a>

the name (id) have to be equal



来源:https://stackoverflow.com/questions/37130590/angular-ui-router-passing-params-between-states

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!