5.3 Components — Passing Properties to A Component
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的更多相关文章
- [Angular 2] Passing data to components with 'properties'
Besides @Input(), we can also use properties on the @Component, to pass the data. import {Component, ...
- 5.4 Components -- Wrapping Content in A Component(在组件中包裹内容)
1.有时候,你可能希望定义一个模板,它包裹其他模板提供的内容. 例如,假设我们创建一个blog-post模板,我们可以使用它来展现一个blog post: app/components/blog-po ...
- Ember.js学习教程 -- 目录
写在前面的话: 公司的新项目需要用到Ember.js,版本为v1.13.0.由于网上关于Ember的资料非常少,所以只有硬着头皮看官网的Guides,为了加深印象和方便以后查阅就用自己拙劣的英语水平把 ...
- [转] 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 ...
- [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 ...
- [Vue @Component] Dynamic Vue.js Components with the component element
You can dynamically switch between components in a template by using the reserved <component> ...
- 6、二、App Components(应用程序组件):1、Intents and Intent Filters(意图和意图过滤器)
1.Intents and Intent Filters(意图和意图过滤器) 1.0.Intents and Intent Filters(意图和意图过滤器) An Intent is a messa ...
- [React Fundamentals] Introduction to Properties
This lesson will teach you the basics of setting properties in your React components. class App exte ...
- 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 ...
随机推荐
- Excel时间格式修改为文本格式
- Effective C++ Item 12 Copy all parts of an object
This one is simple, do not forget to copy all parts of an object in copy constructor or assignment o ...
- ch5-处理数据,抽取-整理-推导
场景:教练kelly有4个选手James\Sarah\Julie\Mikey,他们每跑600米,教练就会计时并把时间记录在计算机的一个文件中,总共4个文件:James.txt\Sarah.txt\Ju ...
- docker中文、手册、教程
Docker资源 Docker官方英文资源: docker官网:http://www.docker.com Docker windows入门:https://docs.docker.com/windo ...
- ajax 跨域访问 :Access-Control-Allow-Origin
一说到ajax跨域.首先想到的就是jsonp . JSONP方法是一种非官方方法,而且这种方法只支持GET方式,不如POST方式安全. 即使使用jQuery的jsonp方法,type设为POST,也 ...
- linux如何设置用户权限
linux与用户权限设置: 1.添加用户 首先用adduser命令添加一个普通用户,命令如下: #adduser tommy //添加一个名为tommy的用户 #passwd tommy //修改密码 ...
- scss的安装使用
Ruby的安装 如果是Window系统,请打开:http://rubyinstaller.org/downloads/ ,下载当前稳定版本的exe文件.界面如下所示: Step(2): 接下来,在系统 ...
- c# Use NAudio Library to Convert MP3 audio into WAV audio(将Mp3格式转换成Wav格式)
Have you been in need of converting mp3 audios to wav audios? If so, the skill in this article prov ...
- SSH电力项目三 - Dao层、service层查询实现(HQL)
底层方法封装:模糊查询,姓张的人 查询思路:select * from elec_text o #Dao层 where o.textName like '%张%' ...
- LCA(离线算法)
hdu4547 CD操作 Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others) To ...