Dynamically set the value of ui-sref Angularjs

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="https://stackoverflow.com/questions/24349731/{{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 🙂

Leave a Comment