EmberJS 将属性传递到组件
2018-01-03 13:58 更新
将属性传递到组件
组件不直接在模板范围中访问属性。要解决这个问题,只需在组件减速时声明属性({{my-comp title=title}})。这只是,组件模板中可用的外部模板范围中的title属性与title属性的名称相同。
<script type="text/x-handlebars">
//component name
{{my-comp title=title}}
</script>
<script type="text/x-handlebars" data-template-name="components/my-comp">
// This is component
</script>
在上面的代码中,'my-comp'组件具有'title'属性,并且使用与属性名('title')相同的名称初始化。
例子
<!DOCTYPE html>
<html>
<head>
<title>Emberjs Passing Properties to a Component</title>
<!-- CDN's-->
<script src="/attachements/w3c/handlebars.min.js"></script>
<script src="/attachements/w3c/jquery-2.1.3.min.js"></script>
<script src="/attachements/w3c/ember.min.js"></script>
<script src="/attachements/w3c/ember-template-compiler.js"></script>
<script src="/attachements/w3c/ember.debug.js"></script>
<script src="/attachements/w3c/ember-data.js"></script>
</head>
<body>
<script type="text/x-handlebars" data-template-name="index">
<!-- passing title property to a component -->
{{my-name title=title}}
</script>
<script type="text/x-handlebars" data-template-name="components/my-name">
<!-- this is the 'my-name' component -->
<p><b>The details of the object passed are</b> :{{title}}</p>
<p><b>Enter your data:</b>{{input type="text" value=title}}</p>
</script>
<script type="text/javascript">
App = Ember.Application.create();
App.IndexRoute = Ember.Route.extend({
model: function() {
//assigning the default value for 'title' property
return {
title: "Tutorialspoint"
};
}
});
</script>
</body>
</html>
输出
让我们执行以下步骤,看看上面的代码如何工作:
将上述代码保存在 comp_passng_prprt.html 文件中
在浏览器中打开此HTML文件。
以上内容是否对您有帮助:

免费 AI IDE


更多建议: