B
B
beloveKSM2015-08-04 18:14:37
Angular
beloveKSM, 2015-08-04 18:14:37

How to make connection between url and global parameter via $stateProvider for pages not dependent on parameter?

There is a global parameter: city. It may or may not be chosen. And there are pages that depend on the selected city, and some that do not. If a city is selected, then its id must be written in the url. If it is not selected, then it is not necessary to write. How to organize those pages for which the choice of the city is not important, for example, entering a password?

var fsApp = angular.module('fsApp', [ 'ui.router']) ;

fsApp.config(function($stateProvider, $urlRouterProvider) {
$urlRouterProvider.otherwise("/main");
  $stateProvider  
     .state('main', {url: '/main', templateUrl: 'main.html', controller : ''})
     .state('street',{url: '/cityId:cityId/street', templateUrl: 'street.html', controller : ''})
     .state('login', {url: '/login', templateUrl: 'login.html', controller : ''})
});

fsApp.value('Global', {
  city: {
      cityId: 0,
      title: 'Выберите город'
     }	  
});

fsApp.run(function ($rootScope, $state, Global) 
{
  $rootScope.$watch('Global.city.cityId', function (newValue, oldValue) 
  {
    if (newValue && !$state.current.abstract) 
    {
                       $state.go($state.current.name); // Как сделать правильный роутинг ???? 
    }
  });
        
  $rootScope.$on('$stateChangeStart', function(event, toState, toParams, fromState, fromParams)
  {
    if (toParams.cityId && toParams.cityId !== Global.city.cityId ||
        !toParams.cityId && Global.city.cityId!==0) 
    {
      if (toParams.cityId && toParams.cityId>0)
      {
        Global.city.cityId = toParams.cityId;
        Global.city.titke= // получить название города
      }
      else
      {
        Global.city.cityId = 0;
        Global.city.title='Выберите город';				
      }	
    } 
  });    
});

fsApp.controller('fsMainCtrl', function($scope, Global)
{	
  $scope.city = Global.city;	
        $scope.selectCity = function(){....}
}

<body ng-controller="fsMainCtrl">
...
     <a href="javascript:void(0)" title="Выбор города" ng-click="selectCity()">{{city.title}}</a>
...
    <div ui-view></div>
...
</body>

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question