1

This is in continuation to the stakoverflow question below

Set different hyperlink for same <a> tag (Using AngularJs)

I have 2 URLs as shown below. Pre.html and Block.html

<ul class="dropdown-menu">
                <li><a ng-model="data" ng-href="{{ data ? 'Pre.html' : 'Block.html' }}" target="_blank">Pre or Bloc</a></li> 
</ul>

This basically checks the value of data in the controller and loads Pre.html if $scope.data is true. It loads Block.html if $scope.data is false.

So, with this i am able to achieve the following

1) User clicks on link. User is shown block page if value of 'data' is false at the controller

2) User clicks on link. User is shown pre.html page if value of 'data' is true at the controller.

But when user type the link in URL, he is still able to access the Pre.html even when the $scope.data is set as false.

How can i block this URL access when $scope.data is set as false. > any suggestions

1
  • You will need to add logic in the route's controller to either prevent the user from seeing the page or reroute them if the data and the page don't match. Commented Apr 5, 2016 at 20:47

1 Answer 1

3

This will not be achievable since when the user types in the URL himself; your $scope changes and is no longer accessible.

One way around this is to set the value in the $window variable instead; this can be pinged from the controller you defined for Pre.html.

For example:

var controllers = angular.module('controllers', []);
controllers.controller('PageCtrl', [ '$scope', '$window', $location, function($scope, $window, $location) {
    if (some condition) {
        $window.sessionStorage.data = true
    } else {
        $window.sessionStorage.data = false
    }
}

If the condition is false, just redirect the user back to the previous page or a default homepage.

controllers.controller('PreCtrl', [ '$scope', '$window', $location, function($scope, $window, $location) {
    if ($window.sessionStorage.data) {
        // Do something?
    } else {
        $location.path('#/home');
    }
}
Sign up to request clarification or add additional context in comments.

Comments

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.