sourcetip

Angular를 사용하여 URL에 검색 매개 변수를 추가하는 방법JS UI 라우터 .go()?

fileupload 2023. 2. 9. 22:26
반응형

Angular를 사용하여 URL에 검색 매개 변수를 추가하는 방법JS UI 라우터 .go()?

UI-Router's를 사용할 때 URL에 검색 파라미터를 추가하는 방법이 있습니까?$state.go()? 동일한 설정으로 새 경로를 정의하는 대신 URL에 추가 정보가 포함된 정의된 상태를 사용합니다.

심플한 뷰가 정의되어 있습니다.

.when('page-with-form', {
    template: 'views/page-with-form.html',
    url: '/page-with-form'
})

누군가가 로 이동할 때 루트가 정상적으로 작동하기를 원합니다./page-with-form그러나 어플리케이션에 추가 정보와 함께 사용자를 해당 루트로 리다이렉트할 수 있는 다른 것이 있는 경우/page-with-form?error=true아마 이렇게 될 겁니다.

$state.go('page-with-form', '?error=true');

이 방법은 다음과 같은 방법으로 해결할 수 있습니다.ui-router- 작업 예시

  $stateProvider
    .state('MyState', {
      url: '/page-with-form?error',
      templateUrl: 'view.page-with-form.html',
      controller: 'MyCtrl',
    })

이 상태로의 네비게이션은 다음과 같습니다.

  // href
  <a href="#/page-with-form">
  <a href="#/page-with-form?error=true">
  <a href="#/page-with-form?error=false">

  //ui-sref
  <a ui-sref="MyState({error:null})">
  <a ui-sref="MyState({error:true})">
  <a ui-sref="MyState({error:false})">

  // state.go() - in fact very similar to ui-sref directive
  $state.go('MyState', {error: null})
  $state.go('MyState', {error: true})
  $state.go('MyState', {error:false})

URL 매개 변수에 대한 문서:

파라미터는 '?' 뒤에 이어지는 쿼리 파라미터로 지정할 수도 있습니다.

url: "/contacts?myParam"
// will match to url of "/contacts?myParam=value"

여기서 동작 테스트

제가 알기로는, 당신은 선택적 쿼리 파라미터를 찾고 있습니다.다행히 쿼리 파라미터는 기본적으로 옵션입니다.시험해 보세요:url: '/page-with-form?error'

.when('page-with-form', {
    template: 'views/page-with-form.html',
    url: '/page-with-form?error'
});

그리고 사용$state.go('page-with-form', {error:true});

언급URL : https://stackoverflow.com/questions/21923582/how-to-add-search-parameter-to-the-url-using-angularjs-ui-router-go

반응형