2016-06-30 4 views
4

:각도 ui.router 동적 UI-SREF 내가 2 개 주와 응용 프로그램이

: settings

  • profile
  • 내가 내 모든 응용 프로그램에 반복되는 링크가를
    <a ui-sref="profile({id:profile.id})" ui-sref-active="active">Current state + ID</a> 
    

    ui-sref을 어떻게 동적으로 변경할 수 있습니까? - 현재 stateParam과 함께 현재 상태를 나타 내기 위해 (id)

    해결책을 찾으면 ui-sref-active을 사용하기를 원합니다. 그래서 ng-click을 피하십시오. 이 같은.

+0

수 너는 정교하니? 동적 변수와 stateParam 변수의 의미가 명확하지 않습니다 ... –

+0

[this] (https://github.com/angular-ui/ui-router/wiki/URL-Routing#url-parameters) 참조 –

+0

사용 $ state.href() 및 ng-href는 다음에서 설명합니다. [http://stackoverflow.com/questions/24349731/dynamically-set-the-value-of-u-sref-angularjs](http://stackoverflow.com)/questions/24349731/동적 설정 - 값 -의 - ui - sref - angularjs) –

답변

6

내 생각에 ui-sref은 (와) 내부에있는 것을 구문으로 분석합니다.

이렇게하면됩니다.

<a ng-repeat="step in steps" ui-sref="{{step.state}}(step.param)"></a> 
+0

당신이 스 니펫을 게시 할 수 있습니까? 잘 보일 지 모르겠지만 작동하는지 ... thou –

+1

[상태 및 매개 변수를 동적으로 ui-sref로 변경] (http://stackoverflow.com/questions/32997064/change-both-state-and-params-dynamically- in-ui-sref) –

+0

감사합니다! 정말 좋은데 ... 오늘 sth 배웠어! –

1

내가 가장 안전하고 이해할 수있는 방법은 간단 if else 사용 같아요

확인
<div ng-if="checkState == 'profile'"> 
     <a ui-sref="profile({id:profile.id})" ui-sref-active="active">Current state + ID</a> 
</div> 

<div ng-if="checkState == 'settings'"> 
     <a ui-sref="settings({id:profile.id})" ui-sref-active="active">Current state + ID</a> 
</div> 

작동합니다 ...

+0

그럴 수는 있지만 그렇게 할 수는 없습니다. 여기에는 2 개 주 (州)가 있지만 액트 럴 앱에는 2 개가 넘습니다. 조건 산을 만들 수는 없습니다. 그것은 좋은 해결책이지만 제 경우에는 그렇지 않습니다. 감사! – user3800799

+0

귀하의 답변은 @Muhammad Usman과 같습니다. –

1
$scope.routers = [ 
    { 
    state: 'profile', 
    params: {id: 1} 
    }, 
    { 
    state: 'settings', 
    params: {id: 1} 
    } 
]; 

보기 :

<a ng-repeat="router in routers" ui-sref="{{router.state}}(router.params)"></a> 
+0

{{router.state}}() -() 괄호도 필요합니다. –