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. list的下标【python】

    转自:http://www.cnblogs.com/dyllove98/archive/2013/07/20/3202785.html list的下表从零开始,和C语言挺类似的,但是增加了负下标的使用 ...

  2. osgEarth2.8添加模型

    #include <osgEarthDrivers/model_simple/SimpleModelOptions> SimpleModelOptions modelOptions; mo ...

  3. 基于麒麟座开发板2.0的MQTT实现例程

    链接--->https://sanwen8.cn/p/649shZ1.html OneNET现已全面适配标准MQTT协议,相信这一功能的增加会**便于开发者进行设备的接入. OneNET提供了M ...

  4. 《C++ Primer Plus》15.1 友元 学习笔记

    15.1.1 友元类假定需要编写一个模拟电视机和遥控器的简单程序.决定定义一个Tv类和一个Remote类,来分别表示电视机和遥控器.遥控器和电视机之间既不是is-a关系也不是has-a关系.事实上,遥 ...

  5. oracle中怎么用normal方式登录怎么自定义用户名和密码

    1.首先要创建一个用户.必须使用有最高权限的用户来创建,语句如下: create user shopping identified by 123456;--创建shopping用户,密码123456 ...

  6. Mysql数据库一个表字段中存了id,并以逗号分隔,id对应的详细信息在另一个表中

    有两张表, 一张为爱好表b表 一张为用户表 u表 u表 id   名称   爱好Id 1    张三     1,2,3,4 2    李四      2,5 b表 id  名称 1    打乒乓 2 ...

  7. synchronized同步语句块

    用关键字synchronized声明方法在某些情况下是有弊端的,比如A线程调用同步方法执行一个长时间的任务,那么B线程则必须等待比较长时间.在这样的情况下可以使用synchronized同步语句块来解 ...

  8. Eclipse failed to get the required ADT version number from the sdk

    failed to get the required ADT version number from the sdk 解决方法: eclipse 和 android studio 工具不能同时共用同一 ...

  9. 巡风代码架构简介以及Flask的项目文件结构简介

    一.巡风: 巡风是一款什么东西,想必安全同行都不陌生吧.用它作为内网漏洞扫描管理架构是一种很好的选择,扫描快,开源,还可自己编写符合规则的POC直接放入相应目录来扩展.今天下午趁着有点时间捋了一下巡风 ...

  10. 关于移动端rem 布局的一些总结

    1.rem是什么? rem(font size of the root element)是指相对于根元素的字体大小的单位 2.为什么web app要使用rem? 实现强大的屏幕适配布局(淘宝,腾讯,网 ...