0

Right now i am making an AngularJS+UI router install application. But i have a problem, the problem is, that i want to disable access to the views, associated with the install application. I want to do it in resolve in the state config.

But the problem is i need to get the data from a RESTful API, whether the application is installed or not. I tried making the function, but it loaded the state before the $http.get request was finished.

Here was my code for the resolve function:

  (function() {
var app = angular.module('states', []);

app.run(['$rootScope', '$http', function($rootScope, $http) {
  $rootScope.$on('$stateChangeStart', function() {
    $http.get('/api/v1/getSetupStatus').success(function(res) {
      $rootScope.setupdb = res.db_setup;
      $rootScope.setupuser = res.user_setup;
    });
  });
}]);

app.config(['$stateProvider', '$urlRouterProvider', function($stateProvider, $urlRouterProvider) {
  $urlRouterProvider.otherwise("/404");

  $stateProvider.state('db-install', {
      url: "/install/db",
      templateUrl: 'admin/js/partials/db-install.html',
      controller: 'DBController',
      resolve: {
        data: function($q, $state, $timeout, $rootScope) {
          var setupStatus = $rootScope.setupdb;
          var deferred = $q.defer();

          $timeout(function() {
            if (setupStatus === true) {
              $state.go('setup-done');
              deferred.reject();
            } else {
              deferred.resolve();
            }
          });
          return deferred.promise;
        }
      }
    })
    .state('user-registration', {
      url: "/install/user-registration",
      templateUrl: "admin/js/partials/user-registration.html",
      controller: "RegisterController"
    })
    .state('setup-done', {
      url: "/install/setup-done",
      templateUrl: "admin/js/partials/setup-done.html"
    })
    .state('404', {
      url: "/404",
      templateUrl: "admin/js/partials/404.html"
    });
}]);
})();

EDIT:

Here is what my ajax call returns: What AJAX returns

1 Answer 1

0

Try this way:

$stateProvider.state('db-install', {
  url: "/install/db",
  templateUrl: 'admin/js/partials/db-install.html',
  controller: 'DBController',
  resolve: {
    setupStatus: function($q, $state, $http) {
      return $http.get('/api/v1/getSetupStatus').then(function(res) {
        if (res.db_setup === true) {
          $state.go('setup-done');
          return $q.reject();
        }
        return res;
      });
    }
  }
})

Then inject setupStatus in controller:

.state('setup-done', {
  url: "/install/setup-done",
  templateUrl: "admin/js/partials/setup-done.html",
  controller: ['$scope', 'setupStatus', function ($scope, setupStatus) {
      $scope.setupdb = setupStatus.db_setup;
      $scope.setupuser = setupStatus.user_setup;
  }]
})
Sign up to request clarification or add additional context in comments.

2 Comments

It still doesn't work it just loads the state anyway
i have added my response data now

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.