简体   繁体   中英

How can I add some logic in my angular routes?

I need to lock some routes in 'case' sections for dependence of $scope variables (of valid or not-valid forms)

var loginForm = angular.module('loginForm',[
  'ngRoute',
  'stepsControllers'
  ]);

loginForm.config(['$routeProvider',
  function($routeProvider) {



    this.returnUrl = function(tempN) {

      switch(tempN) {
        case 1:
          return 'partials/step1.html';
          break;
        case 2:
          return 'partials/step2.html';
          break;
        case 3:
          return 'partials/step3.html';
          break;
        case 4:
          return 'partials/step4.html';
          break;
      }
    };

    $routeProvider.
      when('/step1', {
        templateUrl: this.returnUrl(1),
        controller: 'step1Ctrl'
      }).
      when('/step2', {
        templateUrl: this.returnUrl(2),
        controller: 'step2Ctrl'
      }).
      when('/step3', {
        templateUrl: this.returnUrl(3),
        controller: 'step3Ctrl'
      }).
      when('/step4', {
        templateUrl: this.returnUrl(4),
        controller: 'step4Ctrl'
      }).
      otherwise({
        redirectTo: '/'
      });

  }
]);

I believe that we need to add some service and then later it needs to be added it to config of my controller. But I don't understand DI things and don't understand how to write code to release it?

i suggest $routeParams to organize your urls

angular.module('myApp', []).
    config(function ($routeProvider, $routeParams) {
        $routeProvider.when('/step/:stepId', {
            templateUrl: 'resources/template/'+$routeParams.stepId+'.html',
            controller : 'step'+$routeParams.stepId+'Ctrl'
        });
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM