2017-09-04 63 views
0

当我尝试将数据从父项传递给子组件时,出现错误。无法将角度4中的父数据传递给子组件

这是我把它放在父组件中的标签。

<pop-up [showPopUp]="true" [content]="content" [title]="title goes here"></pop-up> 

子组件

import { Component, OnInit, Input } from '@angular/core'; 


@Component({ 
    selector: 'pop-up', 
    templateUrl: './pop-up.component.html', 
    styleUrls: ['./pop-up.component.css'] 
}) 
export class PopUpComponent implements OnInit { 
    @Input() 
    showPopUp: boolean; 
    @Input() 
    title: string = ""; 
    @Input() 
    content: string = ""; 

    constructor() { 
    } 
    ngOnInit() { 
    debugger; 
    } 
    proceed() { 
    this.showPopUp = true; 
    } 
    closePopUp() { 
    this.showPopUp = false; 
    } 
} 

我想使用的变量showPopUp,标题和内容中的HTML这样

<h5 class="modal-title" id="exampleModalLabel">{{title}}</h5> 
<h5 class="modal-title" id="exampleModalLabel">{{content}}</h5> 

但是当我尝试使用它,我获取错误 enter image description here

我不知道我到底做错了什么。

回答

2
[title]="title goes here" 

应该

[title]="'title goes here'" 

title="title goes here" 

你原来的代码试图将表达式的值赋给title goes heretitle,但就是没有有效的表达。 您可以使表达式成为字符串文字,或删除[],然后Angular不会尝试将该值解释为表达式。

+0

谢谢,我可以知道它采用这种格式的原因吗 –

+0

我在我的回答中已经添加了一个解释(您可能还没有看到它,因为我在我的初始文章后编辑了我的答案)。 '[]'是特殊的Angular绑定语法。 –