-1

I am working with ui-router, as I can achieve several views grouping different html, something like the image shows, ie a view 1 and another view 2. Additionally I want to see 2 only view with an authentication?

Any ideas? enter image description here

My current code:

<div ng-include='"views/header.html"'></div>
<div ui-view></div>
<div ng-include='"views/footer.html"'></div>
2
  • 1
    did you solved or did something with this? Is the answer valid for you? Please, answer to close the question or improve the answers. Thanks!! Commented Nov 2, 2017 at 9:23
  • You seem to have at least one other question on the same topic (perhaps there are more?), but have not accepted an answer on any of them. The way things work around here is that we help you and then you accept an answer that helped, which will help others in future. I was going to upvote these good questions, but don't see I should, if you won't help others. Commented Feb 23, 2020 at 9:18

1 Answer 1

0

Whether authentication on angular must be implemented on ui-router, it´s a little complicated to show it:

Step 1 - Define a variable like "authenticate" in your state provider

angular.module('myApp')
  .config(function ($stateProvider) {
    $stateProvider
      .state('view1', {
        url: '/view1',
        templateUrl: 'app/tests/view1.html',
        controller: 'TestsCtrl1',
        authenticate: false
      });

      $stateProvider
      .state('view2', {
        url: '/view2',
        templateUrl: 'app/views/view2.html',
        controller: 'TestsCtrl2',
        authenticate: true
      });
  });

Step 2 - Change in client/app.js the method for $stateChangeStart. This will detect an stateChange, and check if the destination state has the authenticated value on it. I did it in the run() function of the app

// Redirect to login if route requires auth and you're not logged in
    $rootScope.$on('$stateChangeStart', function (event, next) {
      Auth.isLoggedInAsync(function(loggedIn) {
        //Check if next view (ui-state) requires authentication
        if (next.authenticate && !loggedIn) {
          event.preventDefault();
          $rootScope.lastState=next.name;
          $state.go('login');
        } 
      });
    });

Step 3 - Create Different html template for each view:

//view1.html

<div ng-include='"views/headerA.html"'></div>
<div ng-include='"views/contentA.html"'></div>
<div ng-include='"views/footerA.html"'></div>

//view2.html

<div ng-include='"views/headerB.html"'></div>
<div ng-include='"views/contentB.html"'></div>
<div ng-include='"views/footerB.html"'></div>

Hope it helps

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.