Angular2中的输入属性绑定

重构

以下是具有@Input()属性的组件的代码

import { Component } from '@angular/core';

@Component({
selector: 'cd',
template: ` 
<h2>{{input1}}</h2>

`
})

export class cdComponent{

 @Input() input1: string;

}

我想在其他组件中使用此组件,下面将介绍父组件的代码。

import { Component } from '@angular/core';

@Component({
selector:'main',
template: `
<cd [input1]="test"></cd>
<h2> Main Route </h2>
`
})

export class MainComponent{

}

MainComponent显示在浏览器中时,我期望子组件中的“测试”文本也能显示。

但是没有显示“测试”文本。任何帮助,将不胜感激。

斯蒂芬·斯沃科塔(Stefan Svrkota)

它不显示,因为你逝去的命名变量testinput1的,但它不存在。当您使用[]将值传递给输入时,它会查找名为的变量test,我的猜测是您只想传递string test有两种方法可以做到这一点:

<cd [input1]="'test'"></cd>

或者:

<cd input1="test"></cd>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章