Emberjs-temp-conditon-if

提供:Dev Guides
移動先:案内検索

EmberJS-テンプレート条件If

_#if_ステートメントはブール式を使用します。ブール式がtrueの場合、 if ステートメント内のコードブロックが実行されます。ブール式がfalseの場合、 else ブロックが実行されます。

構文

{{#if property-name}}
  //statement
{{else}}
  //statement
{{/if}}

以下に示す例は、Ember.jsのif条件付きヘルパーの使用方法を示しています。 次のコードで_app/templates/_の下に_application.hbs_というテンプレートを作成します-

{{#if check}}
  //true block of statement
   <h3> boolean value is {{check}}</h3>
   {{else}}
  //false block of statement
   <h3>boolean value is {{check}}</h3>
{{/if}}

次に、_application.js_ファイルと呼ばれるコントローラーを作成します。このファイルは、_app/controller/_の下に次のコードで定義されます-

import Ember from 'ember';

export default Ember.Controller.extend ({
   bool: true,
   check: function () {
     //returning the boolean value to the called function
      return this.bool;
   }.property('content.check'),
});

出力

エンバーサーバーを実行すると、次の出力が表示されます-

Ember.jsテンプレート条件If