[英]Angular 2 RC5 ngStyle data does not propagate from parent component
I have the following simplest Angular 2 application: app.component.ts 我有以下最简单的Angular 2应用程序:app.component.ts
import { Component } from '@angular/core';
@Component({
selector: 'my-app',
template: `
<h1>Test</h1>
<test-component [Height] = "300" [Width]="300"></test-component>`
})
export class AppComponent {
public constructor() {}
}
and the actual test component: 以及实际的测试组件:
import { Component, OnInit, Input } from '@angular/core';
@Component({
selector: 'test-component',
template: `
<div [ngStyle]="{'height' : Height + 'px', 'width' : Width + 'px'}"></div>
`
})
export class TestComponent implements OnInit {
@Input() Height : any;
@Input() Width : any;
constructor() { }
ngOnInit() { }
}
However, the ngStyle data does not propagate to the test component. 但是,ngStyle数据不会传播到测试组件。 Am I doing something wrong or this got broken in RC5?
我做错了什么,还是在RC5中被破坏了?
Thanks 谢谢
You haven't declared TestComponent
as a directive. 您尚未将
TestComponent
声明为指令。
You must declare it as a directive in AppComponent
in order to work. 您必须在
AppComponent
中将其声明为指令才能正常工作。 Your code should look like this in AppComponent
class: 您的代码在
AppComponent
类中应如下所示:
import { Component } from "@angular/core";
import {TestComponent} from "./test.component";
@Component({
selector: "app",
directives: [TestComponent],
template: `<div>{{message}}</div>
<test-component [Height] = "500" [Width]="300"></test-component>`
})
export class AppComponent {
public constructor() {}
}
Pass the TestComponent
in directives
property of the Component
decorator. 在
Component
装饰器的directives
属性中传递TestComponent
。
Update 更新资料
As @JB Nizet correctly pointed out, declaring directives in the component level is marked for deprecation and will be removed from the 2.0.0-final version, in order to favor the use of NgModules as a place to declare your directives. 正如@JB Nizet正确指出的那样,在组件级别中声明指令已标记为已弃用,并将从2.0.0-final版本中删除,以便使用NgModules作为声明指令的位置。 You can refer to the Angular2 Blog to get more info on that.
您可以参考Angular2博客以获得更多信息。
In a new and updated plunk, I have updated the code, so you declare your directive on NgModule
. 在一个新的更新的插件中,我更新了代码,因此您在
NgModule
上声明了指令。
NgModule
: NgModule
:
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import {TestComponent} from "./test.component";
@NgModule({
imports: [ BrowserModule ],
declarations: [ AppComponent, TestComponent ],
bootstrap: [ AppComponent ]
})
export class AppModule { }
Component
: Component
:
import { Component } from "@angular/core";
@Component({
selector: "app",
template: `<div>{{message}}</div>
<test-component [Height] = "500" [Width]="300"></test-component>`
})
export class AppComponent {
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.