Is it possible to change route system ($ route) in the same URL? Angular JS

2

I'm wanting, from a constant (configuration) change of controller or directive that is in the same URL.

For example, imagine that I'm on the home page and then we have the path "/" ok. However, depending on the configuration the face can change that. I'll put an example:

angular.module('meuModulo')
  .config(['$routeProvider’, ‘go.svc.config’,  function($routeProvider, config) {
    $routeProvider
    .when('/', {
      templateUrl: 'directives/directive1.html',
      controller: function() {
    var conf = config.home;

    if (conf === ‘musica’) {
        return 'controllers/meuControllerMusica',
    } else {
        return ‘controllers/meuControllerHomePage';
    }
   };
  })
}]);

The idea here would be: According to a specific configuration, I would change the Controller .... so far perfect.

The problem that .config in this case only accepts $routerProvider , so I can not do what I want.

The closest solution I came up with was to leave a fixed controller (not a function ) and inside it do something like:

$route.current.controller = 'meuControllerMusica';
$route.reload();

The problem is that if it does, it stays in an infinite loop, because it will always stay in the old controller, make the change and give the reload.

Has anyone ever had something similar or would you have any other ideas?

    
asked by anonymous 11.08.2014 / 23:30

2 answers

1

You can use ng-switch to swap a DOM element that contains a directive.

For example:

angular.module('meuModulo')
  .config(['$routeProvider’, ‘go.svc.config’,  function($routeProvider, config) {
    $routeProvider
    .when('/', {
      templateUrl: 'directives/directive1.html',
      controller: 'main'
   };
  })
}])
    .controller('main', ['$scope', 'go.svc.config', function($scope) {
        var conf = config.home;

        if (conf === ‘musica’) {
            $scope.showView = 'musica';
        } else {
            $scope.showView = 'home';
        }
}]);
;

In the view

<div ng-controller="main">
    <div ng-switch on="showView">
      <div  ng-switch-when="home" ng-controller="meuControllerHomePage">Settings Div</div>
      <div  ng-switch-when="musica" ng-controller="meuControllerHomePage">Home Span</div>
      <div  ng-switch-default>default</div>
  </div>
</div>
    
19.08.2014 / 14:23
0

Actually the native router of AngularJS has some disadvantages, I advise you to use the Angular Ui-Router.

Follow the link for a response from me about it, take a closer look, I hope it helps:

How To Use Angular UI Router ? and what are the advantages?

    
21.08.2014 / 22:39