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. SQLServer------存储过程的使用

    转载: http://www.cnblogs.com/hoojo/archive/2011/07/19/2110862.html 例子: 1.学生表 CREATE TABLE [dbo].[Stude ...

  2. Redis(八)-- Redis分布式锁实现

    一.使用分布式锁要满足的几个条件 系统是一个分布式系统(关键是分布式,单机的可以使用ReentrantLock或者synchronized代码块来实现) 共享资源(各个系统访问同一个资源,资源的载体可 ...

  3. VS2015编译JPEG9b源码

    输入:nmake -f makefile.vc libjpeg.lib 出现错误:找不到win32.mak文件 按照网上说的,这个win32.mak可以在C:\Program Files (x86)\ ...

  4. 改变PS1的颜色

    我们能够通过配置PS1变量使提示符成为彩色.在PS1中配置字符序列颜色的格式为:       \[\e[F;Bm\]       基本上是夹在 "\e["(转义开方括号)和 &qu ...

  5. 在navicat中新建数据库

    前言: 在本地新建一个名为editor的数据库: 过程: 1.: 2.选择:utf8mb4 -- UTF-8 Unicode字符集,原因在于:utf8mb4兼容utf8,且比utf8能表示更多的字符. ...

  6. Nutch URL过滤配置规则

    nutch网上有不少有它的源码解析,但是采集这块还是不太让人容易理解.今天终于知道怎么,弄的.现在把crawl-urlfilter.txt文件贴出来,让大家一块交流,也给自己备忘录一个. # Lice ...

  7. eclipse导入maven项目时报Could not calculate build plan: Plugin org.apache.maven.plugins:maven-resources

    在用Eclipse IDE for Java EE Developers进行maven项目的开发时,报错Could not calculate build plan: Plugin org.apach ...

  8. Docker源码分析(三):Docker Daemon启动

    1 前言 Docker诞生以来,便引领了轻量级虚拟化容器领域的技术热潮.在这一潮流下,Google.IBM.Redhat等业界翘楚纷纷加入Docker阵营.虽然目前Docker仍然主要基于Linux平 ...

  9. 在linux下安装wordpress

    win下的简直傻瓜式操作:xampp打包一键安装 linux下的考虑到一些权限问题 还是有点蛋疼的 现在把流程贴出来做下记录: linux下安装xampp和wordpress的流程 ×由于linux下 ...

  10. C语言学习链接

    C语言博客链接: http://www.cnblogs.com/ningvsban/category/585944.html