简体   繁体   English

使用输入绑定将数据从父组件传递到子组件时出错

[英]Error during passing data from parent to child component with input binding

I am trying to pass data from parent component to child component using input binding. 我正在尝试使用输入绑定将数据从父组件传递到子组件。 It is a simple boolean field. 这是一个简单的布尔字段。 I followed this Angular tutorial to accomplish it but I am still not able to get anything from parent to child component. 我按照这个Angular 教程来完成它,但仍然无法从父组件到子组件获得任何东西。

Also I dont know if its worth mentioning, at first I was getting the below error then I added schemas: [NO_ERRORS_SCHEMA] in my app module and it went away. 我也不知道是否值得一提,起初我遇到以下错误,然后在我的应用模块中添加了模式:[NO_ERRORS_SCHEMA],但它消失了。 在此处输入图片说明

Below is my parent component.ts code for the variable I want to pass: 以下是我要传递的变量的父component.ts代码:

 public isAnAuthorizedUser: boolean = true;

   ngOnInit(): void {

        if (test != null) {
            console.log("Inside NGOnInit on page 1");
            this.route.params
                .switchMap((params: Params) => this._Service.getRequestById(+params['id']))
                .subscribe(
                data => {
                    this.Model = data;
                    if (this.Model.isAuthorizedToView != true)
                    {
                        this.isAnAuthorizedUser = false;
                        this._router.navigate(['/dashboard']);
                    }
        }

Here is my parent component html that I am trying to pass. 这是我要传递的父组件html。

 <div>
                <isAnAuthorizedUser [data]="isAnAuthorizedUser"></isAnAuthorizedUser>
            </div>

Below is my child component TS file code: 以下是我的子组件TS文件代码:

   import { Component, OnInit, Input, Injectable, ViewChild  } from '@angular/core'
import {
    FormsModule,
    ReactiveFormsModule,
    FormGroup,
    FormControl
} from '@angular/forms';
import { HttpModule } from '@angular/http';

@Component({
    moduleId: module.id,
    selector: 'ReservationFormPage1',
    templateUrl: './ReqDashboard.html'
    ,
    providers: [ReqFormService, CompleteRequestService, IncompleteRequestService]
})

export class ReservationDashboardComponent implements OnInit {

    @Input()
    isAnAuthorizedUser: boolean;
                ngOnInit(): void {
                    console.log(this.isAnAuthorizedUser);
    }

}

Below is the HTML code where I am testing to access my pass in varianble: 以下是我测试要在varianble中访问我的通行证的HTML代码:

 <div *ngIf ="isAnAuthorizedUser">
        <span>{{this.isAnAuthorizedUser}}</span>
    </div>

the property name in html should match the name of your @Input property name. html中的属性名称应与@Input属性名称的名称匹配。

try below, 试试下面,

<isAnAuthorizedUser [isAnAuthorizedUser]="isAnAuthorizedUser"></isAnAuthorizedUser>

You should do in your component: 您应该在组件中执行以下操作:

@Input('data') isAnAuthorizedUser;

and then in your template 然后在您的模板中

<isAnAuthorizedUser [isAnAuthorizedUser]="true"></isAnAuthorizedUser>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM