EmberJS 組件自定??義屬性

2018-01-04 11:24 更新

描述

您可以使用將屬性綁定到DOM元素的attributeBindings屬性來自定義屬性。

語句

Ember.Component.extend({
   tagName: 'Valid HTML5 tag',
   attributeBindings: ['AttributeName'],
   AttributeName: "ValueForAttribute"
});

例子

<!DOCTYPE html>
<html>
   <head>
      <title>Emberjs Customizing Attributes</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="application">
         <h3>Customizing HTML 'font' tag Attribute</h3>
         <!-- specfying the component name with title var -->
         {{my-tag title=title}}
      </script>

      <script type="text/x-handlebars" data-template-name="components/my-tag">
         <!-- displaying the title value -->
         {{title}}
      </script>

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

         App.ApplicationRoute = Ember.Route.extend({
            model: function() {
               return {
                  title: 'Welcome to Tutorialspoint...'
               };
            }
         });

         App.MyTagComponent =Ember.Component.extend({
            //specifying the tag name property as 'font'
            tagName: 'font',
            //binding the 'color' attribute in the 'font' tag
            attributeBindings: ['color'],
           //specifying the value for 'color' attribute as 'red'
            color: "red"
         });
      </script>
   </body>
</html>

輸出

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

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

  • 在瀏覽器中打開此HTML文件。

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

掃描二維碼

下載編程獅App

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

編程獅公眾號(hào)