1. 默认情况下,一个组件在它使用的模板范围中没有访问属性。

例如,假想你有一个blog-post组件被用来展示一个blog post:

app/templates/components/blog-post.hbs

<h1>Component: {{title}}</h1>
<p>Lorem ipsum dolor sit amet.</p>

你可以看到它有一个{{title}}Handlebars表达式去打印title属性的值到<h1>

2. 现在设想我们有下面的模板和路由:

app/routes/index.js

export default Ember.Route.extend({
model() {
return {
title: "Rails is omakase"
};
}
});

app/templates/index.hbs

<h1>Template: {{title}}</h1>
{{blog-post}}

运行这份代码,你会发现第一个<h1>(来自外面的模板)展现title属性,但是第二个<h1>(来自组件的)是空的。

3. 我们可以修复它通过使title属性对组件可用:

{{blog-post title=title}}

这样,通过使用相同的名字title使外面模板范围的title属性在组件模板中可用。

4. 如果,在上面例子中model的title属性被name属性替代了,我们可以改变模板的用法:

{{blog-post title=name}}

换句话说,通过使用componentProperty=outerProperty,你绑定一个来自外部范围的命名属性到内部范围的命名属性。

5. 要注意,这些属性的值是被绑定的。不管你改变这个值是在model中还是在组件内部,值保持同步。在下面的例子中,在text field中输入一些文本,无论是在外部模板还是在组件内部,主要它们是如何保持同步的。

6. 你也可以绑定来自一个{{#each}}循环中的属性。这将为每一条数据创建一个组件并且为循环中的每一个model绑定它。

{{#each model as |post|}}
{{blog-post title=post.title}}
{{/each}}

7. 如果你使用{{component}}辅助器来渲染你的模板,你可以用相同的方式传递属性。

{{component componentName title=title name=name}}

5.3 Components — Passing Properties to A Component的更多相关文章

  1. [Angular 2] Passing data to components with 'properties'

    Besides @Input(), we can also use properties on the @Component, to pass the data. import {Component, ...

  2. 5.4 Components -- Wrapping Content in A Component(在组件中包裹内容)

    1.有时候,你可能希望定义一个模板,它包裹其他模板提供的内容. 例如,假设我们创建一个blog-post模板,我们可以使用它来展现一个blog post: app/components/blog-po ...

  3. Ember.js学习教程 -- 目录

    写在前面的话: 公司的新项目需要用到Ember.js,版本为v1.13.0.由于网上关于Ember的资料非常少,所以只有硬着头皮看官网的Guides,为了加深印象和方便以后查阅就用自己拙劣的英语水平把 ...

  4. [转] React Native Navigator — Navigating Like A Pro in React Native

    There is a lot you can do with the React Native Navigator. Here, I will try to go over a few example ...

  5. [Vue] Use Vue.js Component Computed Properties

    You can add computed properties to a component to calculate a property on the fly. The benefit of th ...

  6. [Vue @Component] Dynamic Vue.js Components with the component element

    You can dynamically switch between components in a template by using the reserved <component>  ...

  7. 6、二、App Components(应用程序组件):1、Intents and Intent Filters(意图和意图过滤器)

    1.Intents and Intent Filters(意图和意图过滤器) 1.0.Intents and Intent Filters(意图和意图过滤器) An Intent is a messa ...

  8. [React Fundamentals] Introduction to Properties

    This lesson will teach you the basics of setting properties in your React components. class App exte ...

  9. From MSI to WiX, Part 1 - Required properties, by Alex Shevchuk

    Following content is directly reprinted from From MSI to WiX, Part 1 - Required properties Author: A ...

随机推荐

  1. NHibernate初学三之条件查询(Criteria Queries)与AspNetPager分页实例

    NHibernate除了SQL与HQL两种查询操作外,还有一种就是条件查询Criteria,本文将从网上整理一些Criteria的理论及小实例,最后通过一个结合AspNetPager分页来加深理解,必 ...

  2. POJ 1655 Balancing Act(求树的重心--树形DP)

    题意:求树的重心的编号以及重心删除后得到的最大子树的节点个数size,假设size同样就选取编号最小的. 思路:随便选一个点把无根图转化成有根图.dfs一遍就可以dp出答案 //1348K 125MS ...

  3. 使用loadrunner进行压力测试遇到的问题总结

    本人整理了一个LR使用过程中遇到的各种问题的总结文档,有需要可以加QQ群169974486下载. 一.无法生成虚拟用户,运行报错:CCI compilation error -vuser_init.c ...

  4. [分享]方便的 windbg 命令 - !list

    Windows 内部的各种结构通常都会由双向链表串起来,用 !list 命令查看这些结构非常方便. 比如查看系统中的所有进程: lkd> !list -t nt!_LIST_ENTRY.Flin ...

  5. 【顽固BUG】Visual Studio 2015 + TestDriven.NET-3.8.2860_Personal_Beta 调用的目标发生了异常。

    前言 突然怎么弄也无法断点调试了 输出如下: ------ Test started: Assembly: Server5.V2.dll ------ 调用的目标发生了异常. 而且网站运行提示: -- ...

  6. poj_3321 线段树/树状数组

    题目大意 一个果树(每个节点的分叉数目不固定)上有N个分叉点(包括最末的叶节点),则有N-1条边,将分叉点进行从1到N编号,每个分叉点上均可以结水果.开始的时候,每个分叉点都有一个水果,之后进行一系列 ...

  7. 解决 ssh 登录到ubuntu server 慢的问题

    最近在服务器上使用ubuntu系统,服务器上开启sshd服务,在客户端处使用ssh登录到服务器的时候出现卡顿的现象. 在网上搜索了解决方案,主要是: 1. 修改服务器的 /etc/ssh/sshd_c ...

  8. 【黑金原创教程】【TimeQuest】TimeQuest原创教程连载导读【连载完成,共七章】

    [第一章]TimeQuest 静态时序分析模型的概念 [第二章]TimeQuest模型角色,网表概念,时序报告 [第三章]TimeQuest 扫盲文 [第四章]内部延迟与其他 [第五章]网表质量与外部 ...

  9. 微信小程序 --- https请求

    wx.request发起的是 https 请求,而不是 http 请求.一个小程序 同时 只能有 5个 网络请求. 参数: url:开发者服务器接口地址: data:请求的参数: header:设置请 ...

  10. php程序中判断session过期

    我们最常用的是在php程序中设置,如下例程序所示: if(!isset($_SESSION['abc']) || time()-$_SESSION['abc']>60){ $_SESSION[' ...