EmberJS 發(fā)送主操作組件

2018-01-03 19:07 更新

描述

在主操作中,組件發(fā)送一種操作。例如,當(dāng)您單擊按鈕時(shí),它會(huì)向組件發(fā)送操作,這稱為主操作。

語(yǔ)句

{{my-button action="actions"}}

例子

<!DOCTYPE html>
<html>
   <head>
      <title>Emberjs Sending a Primary Action</title>
      <!-- CDN's -->
      <script src="https://cdnjs.cloudflare.com/ajax/libs/handlebars.js/3.0.1/handlebars.min.js"></script>
      <script src="https://code.jquery.com/jquery-2.1.3.min.js"></script>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/ember.js/1.10.0/ember.min.js"></script>
      <script src="https://builds.emberjs.com/tags/v1.10.0-beta.3/ember-template-compiler.js"></script>
      <script src="https://builds.emberjs.com/release/ember.debug.js"></script>
      <script src="https://builds.emberjs.com/beta/ember-data.js"></script>
   </head>
   <body>
      <script type="text/x-handlebars" data-template-name="index">
         <p>{{my-comp title=title action="compFunc"}}</p>
      </script>

      <script type="text/x-handlebars" data-template-name="components/my-comp">
         <h2>Click button for primary action</h2>
         <input type="button" value="Click Me" {{action "compFunc"}} /><br/>
         <p><b>{{title}}</b></p>
      </script>

      <script type="text/javascript">
         App = Ember.Application.create();

         App.MyCompComponent = Ember.Component.extend({
            //defining the action hook to set the title property value
            actions: {
               compFunc: function() {
                  this.set('title', " Say Hello To Tutorialspoint...");
                  //This method sends the specified action when the component is used in a template
                  this.sendAction();
               }
            }
         });
      </script>
   </body>
</html>

輸出

讓我們執(zhí)行以下步驟,看看上面的代碼如何工作:

  • 將上面的代碼保存在comp_prmry_act.html文件中

  • 在瀏覽器中打開(kāi)此HTML文件。

以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)