当前位置: 首页 > 文档资料 > EmberJS 入门教程 >

#if

优质
小牛编辑
115浏览
2023-12-01

#if语句使用布尔表达式,其中,如果布尔表达式为真,则执行if语句中的代码块; 如果布尔表达式为false,则执行else块。

语法 (Syntax)

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

例子 (Example)

下面给出的示例显示了在Ember.js中使用if条件助手。 使用以下代码在app/templates/下创建名为application.hbs app/templates/ -

{{#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'),
});

输出 (Output)

运行ember服务器,您将收到以下输出 -

Ember.js模板条件如果