0

Controller is not initialising on the same page when using $state.go or $location.href

I am using sidemenu in my ionic to app pass category Id to product page using service but data is not updating corresponding to current category Id. When I click aaa I am successfully redirected to product page and it alerts 1. Again form side menu when i click 'bbb' I get no alert. Also if I chose bbb at first then 2 is alerted and vice versa

Side menu Template

<ion-item  nav-clear menu-close ng-click="allproductpage(1)">aaa</ion-item>
<ion-item  nav-clear menu-close ng-click="allproductpage(2)">bbb</ion-item>

Side menu controller

$scope.allproductpage= function(a){
angular.extend(inpsf.inps, {act_cat : a})  // inpsf is service
$location.path('/app/allproducts')
}

Product page

.controller('AllproductsCtrl', function($scope,inpsf)
{
alert(inpsf.inps.act_cat)   
})

P.S I am using $ionicConfigProvider.views.maxCache(0); in my .config

sam
  • 688
  • 1
  • 12
  • 35
  • Possible duplicate of [AngularJS UI Router - change url without reloading state](http://stackoverflow.com/questions/23585065/angularjs-ui-router-change-url-without-reloading-state) – Debug Diva Feb 20 '17 at 08:17

2 Answers2

0

You should pass the parameter inside the state path declaration and listen for $routeChangeSuccess if using $location or $stateChangeSuccess if using $state, since you are already on the '/app/allproducts' when you click the second menu item and the controller is already "in page". Try something like:

.controller('AllproductsCtrl', function($scope,inpsf)
{
    $scope.$on('$routeChangeSuccess', function() {
        alert(inpsf.inps.act_cat)  
    })
})


//where you config your states..
$stateProvider.state('/app/allproducts/:cat', {

Side menu controller

$scope.allproductpage= function(a){
    angular.extend(inpsf.inps, {act_cat : a})  // inpsf is service
    $location.path('/app/allproducts').search({act_cat: a})
}
Daniele
  • 1,063
  • 1
  • 10
  • 22
0

Try to use following way.

$ionicHistory.clearCache().then(function(){ $state.go('yourpath') })
coder
  • 8,346
  • 16
  • 39
  • 53