123

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <body ng-app="lifecycleRouterDemo" ng-controller="routeController as route">
  <div class="container">
    <nav class="navbar navbar-default">
      <div class="container-fluid">
        <div class="navbar-header">
          <span class="navbar-brand">Angular's New Router</span>
        </div>
      </div>
    </nav>
    <div class="view">
      <div class="col-md-3">
        <ul>
          <li><a ng-link="root">Root</a></li>
          <li><a ng-link="first">First</a></li>
          <li><a ng-link="second">Second</a></li>
        </ul>
      </div>
      <div class="col-md-9">
        <ng-viewport></ng-viewport>
      </div>
    </div>
  </div>

  <script type="text/ng-template" id="./components/first/first.html">
    <div>
      <form novalidate name="first.sampleForm">
        <div class="input-group">
          <label for="sampleText"></label>
          <textarea name="sampleText" id="sampleText" cols="30" rows="10" ng-model="first.sampleText" placeholder="Enter text here..."></textarea>
        </div>
        <div>
          <div class="input-group">
            <button class="btn btn-primary" ng-click="first.save()">Save</button>
          </div>
        </div>
      </form>
    </div>
  </script>
  
  <script type="text/ng-template" id="./components/second/second.html">
    <div>
      Congratulations, you have access to this view!!!
    </div>
  </script>
  
  <script type="text/ng-template" id="./components/root/root.html">
    <h3>This is the root view</h3>
    <div>
      <span ng-show="root.hasAccess">You have access to the second view</span>
      <span ng-show="!root.hasAccess">You don't have access to the second view</span>
      <br/>
      <br/>
      To toggle Access to Second View, <button class="btn" ng-click="root.updateUserAccess()">Click here</button>!
    </div>
  </script>
</body>

            
          
!
            
              .view{
  align-content: center;
}
            
          
!
            
              angular.module('lifecycleRouterDemo', ['ngNewRouter'])
  .value('userAccessInfo', {
  hasAccessToSecondComponent: false
})
  .controller('routeController', ['$router', function($router){
    $router.config([
      {
        path: '/', component: 'root'
      },
      {
        path:'/first', component:'first'
      },
      {
        path:'/second', component: 'second'
      }
    ]);
  }])
  .controller('FirstController', ['$window', function($window){
    this.save = function () {
      this.sampleText = "";
    };

    this.canDeactivate = function () {
      if (this.sampleText) {
        var alertResult = $window.confirm('You have unsaved changes. Do you want to move away from this page?');
        return alertResult;
      }

      return true;
    };
  }])
  .controller('SecondController', ['$window', 'userAccessInfo',function ($window, userAccessInfo) {
    this.canActivate = function(){
      var status =  userAccessInfo.hasAccessToSecondComponent;
      if(!status){
        $window.alert('You don\'t have access t this view. redirecting to previous view...');
      }

      return status;
    };
  }])
  .controller('RootController', ['userAccessInfo', function (userAccessInfo) {
    this.hasAccess = userAccessInfo.hasAccessToSecondComponent;

    this.updateUserAccess = function(){
      userAccessInfo.hasAccessToSecondComponent = !userAccessInfo.hasAccessToSecondComponent;
      this.hasAccess = userAccessInfo.hasAccessToSecondComponent;
    }
  }]);

            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.

Console