简体   繁体   English

如何从模板访问属性?

[英]How can I access the attribute from the template?

In this example i'm expecting it to say "hello world" but the world isn't picked up from the saying attribute. 在此示例中,我希望它说“ hello world”,但是该世界不是从saying属性中选取的。

(function () {
    'use strict';

    $(function () {
        // Set up a route that maps to the `filter` attribute
        can.route(':filter');

        // Render #app-template
        $('#app').html(can.view('appTemplate', {}));

        // Start the router
        can.route.ready();
    });

    can.Component.extend({
        tag: "say",
        scope: {
            saying: function(){
                return this.attr('saying')
            },
            greeting: 'salutations'
        },
        template: can.view("sayTemplate")
    });

})();

Templates: 模板:

<div id="app"></div>

<script id="appTemplate" type="text/mustache">
  <b>Hello</b>
  <say saying="world"></say>
</script>

<script id="sayTemplate" type="text/mustache">
    <b>{{saying}}.</b> <i>{{greeting}}</i>
</script>

You need to tell the component that you want to access the attributes plain value like this: 您需要像这样告诉组件您要访问属性纯值:

can.Component.extend({
  tag: "app-say",
  scope: {
    saying: '@',
    greeting: 'salutations'
  },
  template: can.view("sayTemplate")
});

See this Fiddle . 看到这个小提琴 What you eventually might want to do use an observable attribute from your application state instead of the plain string value. 您最终可能想要使用的是应用程序状态中的observable属性,而不是纯字符串值。 This might look like: 可能看起来像:

var appState = new can.Map({
    name: 'World'
});

$(function () {
    // Set up a route that maps to the `filter` attribute
    can.route(':filter');

    // Render #app-template
    $('#app').html(can.view('appTemplate', appState));

    // Start the router
    can.route.ready();
});

can.Component.extend({
    tag: "app-say",
    scope: {
        greeting: 'salutations'
    },
    template: can.view("sayTemplate")
});

And a template like: 和类似的模板:

<div id="app"></div>

<script id="appTemplate" type="text/mustache">
  <b>Hello</b>
  <app-say saying="{name}"></app-say>
  <div><input type="text" can-value="name"></div>
</script>

<script id="sayTemplate" type="text/mustache">
    <b>{{saying}}.</b> <i>{{greeting}}</i>
</script>

This also creates a cross-bound input field that will update the name everywhere whenever you update the text field. 这还会创建一个跨界输入字段,每当您更新文本字段时,该字段都会在任何地方更新名称。 Fiddle is here . 小提琴在这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从.load()的页面访问CSS属性? - How can i access a css attribute from a .load()'ed page? 如何从此Javascript / jQuery函数访问HTML属性? - How can I access a HTML attribute from this Javascript/jQuery function? 如何从调用控制器的指令中访问控制器的属性 - How can I access attribute of controller in directive from calling controller Angular - 如何从 typescript 访问模板变量/引用? - Angular - How can I access Template variable/reference from typescript? 如何在流星模板中访问父母的数据属性? - How Do I Access a Parent's Data Attribute in Meteor Template? 如何在AngularJS中将控制器的传递值返回到指令的模板属性? - How can I get pass value from controller back to directive's template attribute in AngularJS? 如何通过传递param属性来更改聚合物元素模板 - How can I change polymer element template by passing param attribute 如何从多个图像访问多个 alt 属性值? - How can I access multiple alt attribute values from several images? 如何从Javascript上转换的XML访问JSON。@属性 - How can I access JSON.@attribute from an XML Converted on Javascript 如何从javascript访问html的STYLE标记中的class属性? - How can I access the class attribute in STYLE tag of html from javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM