Pen Settings

HTML

CSS

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

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

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

Behavior

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.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <h1>Example: Pub/Sub</h1>
<p>$emitと$broadcastを使ってスコープ間連携を実現します。</p>
<div ng-app="myApp">
  <div ng-controller="mainController as main" class="ctrl">
    <h2>mainController</h2>
    <p>Message: {{ main.message }}</p>
    <p>{{ main.count }}</p>
    
    <div ng-controller="emitController as emit" class="ctrl left">
      <h3>emitController</h3>
      <p>$emitは自身と親のコントローラーにイベントを伝達する。</p>
      <p>Message: {{ emit.message }}</p>
      <p>{{ emit.count }}</p>
      <button ng-click="emit.countUp()">CountUp</button>
    </div>

    <div ng-controller="broadcastController as broadcast" class="ctrl right">
      <h3>broadcastController</h3>
      <p>$broadcastは自身と子のコントローラーにイベントを伝達する。</p>
      <p>Message: {{ broadcast.message }}</p>
      <p>{{ broadcast.count }}</p>
      <button ng-click="broadcast.countUp()">CountUp</button>
      
      <div ng-controller="childController as child" class="ctrl">
        <h4>childController</h4>
        <p>Message: {{ child.message }}</p>
        <p>{{ child.count }}</p>
      </div>
      
      <div ng-controller="childController as child" class="ctrl">
        <h4>childController</h4>
        <p>Message: {{ child.message }}</p>
        <p>{{ child.count }}</p>
      </div>
    </div>
   </div>
</div>

              
            
!

CSS

              
                @import "bourbon";

body {
  padding: 1.5rem;
  
  font-size: .8em;
}

.ctrl {
  @include clearfix();
  padding: .5rem;
  border: 1px solid #aaa;
  box-sizing: border-box;
  
  &.left {
    float: left;
    width: 50%;
  }
  
  &.right {
    float: right;
    width: 50%;
  }
}
              
            
!

JS

              
                (function() {
  "use strict";

  var appName = 'myApp';

  angular
    .module(appName, []);

  angular
    .module(appName)
    .controller('mainController', mainCtrl)
    .controller('emitController', emitCtrl)
    .controller('broadcastController', broadcastCtrl)
    .controller('childController', childCtrl);

  function mainCtrl($scope){
    var vm = this;

    vm.count = 0;
    $scope.$on('CountEvent', countEvent);

    function countEvent(event, data){
      vm.count++;
      vm.message = data;
    }
  }

  function emitCtrl($scope){
    var vm = this;

    vm.count = 0;
    vm.countUp = countUp;
    $scope.$on('CountEvent', countEvent);

    function countUp(){
      $scope.$emit('CountEvent', '$emit: +1');
    }

    function countEvent(event, data){
      vm.count++;
      vm.message = data;
    }
  }

  function broadcastCtrl($scope){
    var vm = this;

    vm.count = 0;
    vm.countUp = countUp;
    $scope.$on('CountEvent', countEvent);

    function countUp(){
      $scope.$broadcast('CountEvent', '$broadcast: +1');
    }

    function countEvent(event, data){
      vm.count++;
      vm.message = data;
    }
  }

  function childCtrl($scope){
    var vm = this;

    vm.count = 0;
    $scope.$on('CountEvent', countEvent);

    function countEvent(event, data){
      vm.count++;
      vm.message = data;
    }
  }
})();

              
            
!
999px

Console