Greatym.com Greatym.com - 4 months ago 84
AngularJS Question

Angular js : How to access $state current page name and params in html

I have multiple pages those I want to change as per states
I am using ui-routes for managing routing.
I want to access $state.current.name in html.

<div ng-repeat="que in questions.Cars">


I want to change dynamically as below

<div ng-repeat="que in questions.$state.current.name">

So that I could traverse page wise questions.

Plunker for complete code http://plnkr.co/edit/Op1QDwUBECAosPUC7r3N?p=preview

Answer

Include the $state service in your controller. Than you can assign this service to a property on your scope.

Code:

$scope.$state = $state;

Then to get the current state in your templates:

$state.current.name

To get the current state and params in your templates use $state.includes(stateName [, params]):

$state.includes('state.name', {id: 1}); // returns true