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 ...
随机推荐
- 九度 1481 Is It A Tree?
题目 给定一个有向图, 判断其是否是一棵树 要求 (1) 除了根节点外, 每个节点只有唯一的前驱 (2) 从根节点出发, 到任何节点有且只有一条路径 思路 1. 要求(1) 可以通过记录每个节点的前驱 ...
- POJ 2923 Relocation(01背包变形, 状态压缩DP)
Q: 如何判断几件物品能否被 2 辆车一次拉走? A: DP 问题. 先 dp 求解第一辆车能够装下的最大的重量, 然后计算剩下的重量之和是否小于第二辆车的 capacity, 若小于, 这 OK. ...
- 苹果降频门:旧款iPhone哪些功能受到影响
要说苹果最近发生的大事,就数网络上传的沸沸扬扬的降频门事件了,近期苹果在新发布的iOS 11系统中新增了一项功能,意在降低旧款手机的电量消耗,但限制了旧款iPhone的性能,那么iPhone有哪些功能 ...
- 安装memcacheq
1.下载memcacheq包 下载地址:http://code.google.com/p/memcacheq/downloads/list 解压包:# tar -zxvf memcache ...
- lua常用操作
1 .Lua生成随机数: Lua 生成随机数需要用到两个函数:math.randomseed(xx), math.random([n [, m]]) 1. math.randomseed(n) 接收一 ...
- AndroidWear开发之下载SDK[Android W/Android L]
Android L Developer Preview SDK发布了,但是天朝还是无法更新到.打开SDK Manager依旧一成不变,这时候就需要利器了. 第一步: 打开Goagent,不要说不知道什 ...
- 【ecshop】 完全清除版权信息
完整去掉版权和后门的方法: 去ECSHOP版权,去官方后门,完整版 作者:阿牛 ECShop是一款B2C独立网店系统,适合企业及个人快速构建个性化网上商店.系统开源但不免费,是基于PHP语言及MYSQ ...
- Excel打开csv文件乱码问题的解决办法
excel打开csv 出现乱码怎么解决 https://jingyan.baidu.com/article/ac6a9a5e4c681b2b653eacf1.html CSV是逗号分隔值的英文缩写,通 ...
- Cisco IOS和IOS XE 新漏洞检测与修复
Cisco IOS/IOS XE 新漏洞检测与修复 CVE-2018-0150 Cisco IOS XE 存在默认弱口令 漏洞影响: 默认弱口令可以导致攻击者直远程登录控制Cisco设备.受影响版本, ...
- Web界面的服务器监测工具(转载)
企业服务器对于企业业务持续性意义重大,系统管理员需要密切关注企业服务器以确保一切正常运行.当发现问题的时候,他们需要知道问题开始出现时的状况,因此调查可以重点放在问题出现的时候,这就意味着定期记录信息 ...