简体   繁体   English

angular2 @input没有将值传递给子组件

[英]angular2 @input not passing values into child component

I am using @angular/cli: 1.0.1 我正在使用@ angular / cli:1.0.1

Problem 问题

I am trying to add css class in my child component from parent component. 我试图在父组件的子组件中添加css class I'm using @input in parent component and provide a css class . 我在父组件中使用@input并提供了一个CSS class I am not getting value from parent component. 我没有从父组件获得价值。 Below is my parent component code. 下面是我的父组件代码。 What I'm doing wrong? 我做错了什么?

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

 @Component({
     selector: 'app-root',
     templateUrl: './app.component.html',
     styleUrls: ['./app.component.scss']
 })
 export class AppComponent {
     title = 'app works!';
     @Input('init') 
     changeClass: string;
 // Below function run when user scroll down the page
   onScroll(event) {
     let number = event.srcElement.scrollTop;
     if (number > 121) {
     this.changeClass = "navbar-fixed";
     } else if (number < 122) {
         this.changeClass = "";
     }
   }
}


<div class="ah-master-page" (scroll)="onScroll($event)">
    class="ah-top-bar">
    <div class="ah-top-bar-section1">
        <i class="fa fa-mobile fa-lg" aria-hidden="true"></i> +92 000 0000000
    </div>
    <div class="ah-top-bar-section2">
        <img src="./assets/images/hut.png" alt="">
        <span class="ah-logo-label">Reservation</span>
        <span style="font-size: 14px;position: relative;top: 
        6px;">.com</span>
    </div>
    <div class="ah-top-bar-section3">
        <i class="fa fa-facebook fa-lg" aria-hidden="true"></i>
        <i class="fa fa-twitter fa-lg" aria-hidden="true"></i>
        <i class="fa fa-google fa-lg" aria-hidden="true"></i>
    </div>
 </div>
 <app-navbar [changeClass] = "_class"></app-navbar>
 <div class="ah-wrapper">
    <div class="ah-component1">
    <app-login></app-login>  
 </div>
 <div class="ah-component2">
      <app-newsfeed  *ngFor="let item of createRange(5);"></app-newsfeed>
 </div>
 <div class="ah-component3">
      <app-hotfeeds></app-hotfeeds>  
 </div>

Below is my child component code where I'am willing to get the class name. 以下是我愿意获得类名称的子组件代码。

Navbar Component HTML 导航栏组件HTML

<div class="ah-navbar {{_class}}">
    <ul>
      <li>
        <a href="javascript:void(0)" class="ah-active"> 
        <i class="fa fa-home" aria-hidden="true"></i>
        </a>
      </li>
      <li>
       <a href="javascript:void(0)">
        <i class="fa fa-binoculars" aria-hidden="true"></i>
        Explore Huts
       </a>
      </li>
    </ul>
</div>

Navbar Component TS 导航栏组件TS

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

@Component({
  selector: 'app-navbar',
  templateUrl: './navbar.component.html',
  styleUrls: ['./navbar.component.scss']
})
export class NavbarComponent implements OnInit {
  _class: string;

constructor() {
  console.log(this._class);
}
}

You should use @Input in you child component. 您应该在子组件中使用@Input This annotation marks fields which this component receives 'from outside' itself. 该注释标记了组件自身“从外部”接收的字段。

import { Component, OnInit } from '@angular/core';
@Component({
  selector: 'app-navbar',
  templateUrl: './navbar.component.html',
  styleUrls: ['./navbar.component.scss']
})
export class NavbarComponent  implements OnInit {
  @Input('changeClass')
  _class: string;

  constructor() {
  }

}

Use @Input in your child component 在子组件中使用@Input

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

@Component({
  selector: 'app-navbar',
  templateUrl: './navbar.component.html',
  styleUrls: ['./navbar.component.scss']
})

export class NavbarComponent implements OnInit {
  @Input() public _class: string;

  public ngOnInit(){
      console.log(this._class)
  }    
}

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

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