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: Promise 02</h1>
<p>複数の非同期処理が完了したら値を表示する(5秒)。</p>
<div ng-app="myApp">
  <div ng-controller="myController">
    <button ng-click="get_all()">すべての情報を取得</button>
    <button ng-click="reset()">リセット</button>
    
    <p>{{ data1 }}</p>
    <p>{{ data2 }}</p>
  </div>
</div>
              
            
!

CSS

              
                @import "bourbon";

body {
  padding: 1.5rem;
}
              
            
!

JS

              
                var service = angular.module("customServices", []);
service.factory('myService', ['$q', '$timeout', function($q, $timeout) {
  return {
    data1: function(){
      //deferのインスタンスを作る(お呪いのようなもの)
      var d = $q.defer();
      
      $timeout(function(){
        d.resolve("Data:1(3秒で取得完了)");
      }, 3000);
      
      //プロミスオブジェクトを返す
      return d.promise;
    },
    data2: function(){
      var d = $q.defer();
      
      $timeout(function(){
        d.resolve("Data:2(5秒で取得完了)");
      }, 5000);
      
      //プロミスオブジェクトを返す
      return d.promise;
    }
  }
}]);

var app = angular.module('myApp', ['customServices']);
app.controller('myController', ['$scope', '$q', 'myService', function($scope, $q, myService){
  
  $scope.get_all = function(){
    //処理中に表示するテキスト
    $scope.data1 = '取得中';
    $scope.data2 = '取得中';
    
    //$q.allを使って複数のPromiseオブジェクトを取得する
    $q.all([
      myService.data1(),
      myService.data2()
    ]).then(function(strs){
      //すべてのPromiseオブジェクトが取得できたら実行される
      $scope.data1 = strs[0];
      $scope.data2 = strs[1];
    });
  }
  
  $scope.reset = function(){
    $scope.data1 = '';
    $scope.data2 = '';
  }
}]);
              
            
!
999px

Console