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. mybatis由浅入深day01_1课程安排_2对原生态jdbc程序中问题总结

    mybatis 第一天 mybatis的基础知识 1 课程安排: mybatis和springmvc通过订单商品 案例驱动 第一天:基础知识(重点,内容量多) 对原生态jdbc程序(单独使用jdbc开 ...

  2. Java精选笔记_Tomcat开发Web站点

    Tomcat开发Web站点 Web开发的相关知识 B/S架构和C/S架构 C/S架构是Client/Server的简写,也就是客户机/服务器端的交互.常见应用 : QQ. 迅雷. 360. 旺旺等 B ...

  3. Effective C++ Item 15 Provide access to raw resources in resource-managing classes

    In last two item, I talk about resource-managing using RAII, now comes to the practical part. Often, ...

  4. GIS-011-Cesium 使用 IIS设置

    .terrain Content-Type='application/octet-stream'

  5. ionic ui框架及creator使用帮助

    UI框架使用方法:http://ionicframework.com/docs/api/ PS:路由之类的其他js代码示例建议用 官方的app 生成器弄一个简单的页面,然后下载回来看 https:// ...

  6. spring AOP底层原理实现——jdk动态代理

    spring AOP底层原理实现——jdk动态代理

  7. 树形dp-hdu-3721-Building Roads

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3721 题目意思: 给一颗树,求移动一条边(边权值不变)到另外的位置,还是一棵树.求最小的树的直径. ...

  8. Java多线程详解(三)

    1)死锁 两个线程相互等待对方释放同步监视器时会出现死锁的现象,这时所有的线程都处于阻塞状态,程序无法继续向下执行. 如下就是会出现死锁的程序. 首先flag = 1,线程d1开始执行,锁住对象o1, ...

  9. JavaWeb温习之Session对象

    1. Session简单介绍 在WEB开发中,服务器可以为每个用户浏览器创建一个会话对象(session对象),注意:一个浏览器独占一个session对象(默认情况下).因此,在需要保存用户数据时,服 ...

  10. JZOJ.5328【NOIP2017模拟8.22】世界线

    Description