Change back button href in controller?

I have a page that have 3 level, when my user reached the last page (3rd level) I want to change the back button so that the user will go back the first level if clicked. Because by default it will go the second page.

//what should I do before or after this code?

Inject $ionicHistory inside the controller of that view and use $ionicHistory.goBack(-2) to go back two views.$ionicHistory/

I don’t want to go back programmatically, I want to set the back button’s target when user clicked on it.