Dynamically set the value of ui-sref Angularjs

前端 未结 12 1742
囚心锁ツ
囚心锁ツ 2020-11-28 04:15

I have searched for a similar question but the ones that came up seem slightly different. I am trying to change the ui-sref=\'\' of a link dynamically (this link points to t

相关标签:
12条回答
  • 2020-11-28 04:47

    Came to answer that for good :)

    Fortunately, you don't need to use a button for ng-click, or use a function inside an ng-href to achieve what you seek. Instead;

    You can create a $scope var in your controller and assign the ui-sref string in it and use it in your view, as ui-sref attribute.

    Like this:

    // Controller.js
    
    // if you have nasted states, change the index [0] as needed.
    // I'm getting the first level state after the root by index [0].
    // You can get the child by index [1], and grandchild by [2]
    // (if the current state is a child or grandchild, of course).
    var page = $state.current.name.split('.')[0];
    $scope.goToAddState = page + ".add";
    
    
    // View.html
    <a ui-sref="{{goToAddState}}">Add Button</a>
    

    That works perfectly for me.

    0 讨论(0)
  • 2020-11-28 04:48
    <a ng-click="{{getLinkUrl({someParam: someValue})}}">Dynamic Link</a>
    
    $scope.getLinkUrl = function(value){
      $state.go('My.State',{someParam:value});
    
    }
    

    It returns an object

    0 讨论(0)
  • 2020-11-28 04:50

    Looks like this is possible to do after all.

    A breadcrumb on GitHub by one of the ui-router authors led me to try the following:

    <a ng-href="{{getLinkUrl()}}">Dynamic Link</a>
    

    Then, in your controller:

    $scope.getLinkUrl = function(){
      return $state.href('state-name', {someParam: $scope.someValue});
    };
    

    Turns out, this works like a charm w/ changing scoped values and all. You can even make the 'state-name' string constant reference a scoped value and that will update the href in the view as well :-)

    0 讨论(0)
  • 2020-11-28 04:51
    <ul class="dropdown-menu">
      <li ng-repeat="myPair in vm.Pairs track by $index">
         <a ui-sref="buy({myPairIndex:$index})"> 
              <span class="hidden-sm">{{myPair.pair}}</span>
         </a>
      </li>
    </ul>
    

    If someone only wants to dynamically set the $stateParams of ui-sref in Angularjs. Note: In inspect element it will still appear as "buy({myPairIndex:$index})" but $index will be fetched in that state.

    0 讨论(0)
  • 2020-11-28 04:54

    There is a working plunker. The most easier way seems to be to use combination of:

    • $state.href() (doc here) and
    • ng-href (doc here)

    These together could be used as:

    <a ng-href="{{$state.href(myStateName, myParams)}}">
    

    So, (following this plunker) having states like these:

    $stateProvider
      .state('home', {
          url: "/home",
          ...
      })
      .state('parent', {
          url: "/parent?param",
          ...
      })
      .state('parent.child', { 
          url: "/child",
          ...
    

    We can change these values to dynamically generate the href

    <input ng-model="myStateName" />
    <input ng-model="myParams.param" />
    

    Check it in action here

    ORIGINAL:

    There is a working example how to achieve what we need. But not with dynamic ui-sref .

    As we can can check here: https://github.com/angular-ui/ui-router/issues/395

    Q: [A]re dynamic ui-sref attributes not supported?
    A: Correct.

    But we can use different feature of ui-router: [$state.go("statename")][5]

    So, this could be the controller stuff:

    $scope.items = [
      {label : 'first', url: 'first'},
      {label : 'second', url: 'second'},
      {label : 'third', url: 'third'},
    ];
    $scope.selected = $scope.items[0];
    $scope.gotoSelected = function(){
      $state.go("form." + $scope.selected.url);
    };
    

    And here is the HTML template:

    <div>
      choose the url:
      <select
        ng-model="selected"
        ng-options="item.label for item in items"
      ></select>
    
      <pre>{{selected | json}}</pre>
      <br />
      go to selected
      <button ng-click="gotoSelected()">here</button>
    
      <hr />
      <div ui-view=""></div>
    </div>
    

    The working example

    NOTE: there is more up to date link to $state.go definition, but the deprecated one is a bit more clear to me

    0 讨论(0)
  • 2020-11-28 04:55

    Take a look in this issue #2944.

    The ui-sref doesn't watch the state expression, you can use ui-state and ui-state-params passing the variable.

      <a data-ui-state="selectedState.state" data-ui-state-params="{'myParam':aMyParam}">
           Link to page {{selectedState.name}} with myParam = {{aMyParam}}
      </a>
    

    Also a quickly demo provided in the ticket.

    0 讨论(0)
提交回复
热议问题