【发布时间】:2019-08-28 14:54:47
【问题描述】:
我正在做一个有角度的项目。所以我有一个复杂的样式对象,我将它绑定到我的角度组件。
app.component.ts
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
style:any;
ngOnInit() {
this.style = {
radius:15,
fill: {
color: "red"
},
stroke: {
color: "blue",
width: 3
}
}
}
}
app.component.html
my-component 获取对象并将其与第三方对象一起使用。
my.component.ts
@Component({
selector: 'my-component',
template: `<ng-content></ng-content>`,
})
export class MyComponent implements AfterContentInit {
style: any;
thirdPartyObject: Style;
ngOnInit(): void {
this.style = new Style({
radius: style.radius,
fill: new Fill({ color: style.fill.color }),
stroke: new Stroke({ color: style.stroke.color, width: style.stroke.width })
});
}
ngAfterContentInit(): void {
thirdPartyObject.setStyle(function(){
return this style;
})
}
}
我正在使用来自 app.component 的样式对象创建第三方样式对象。而thirdPartyObject 有一个名为setStyle() 的函数来获取样式对象。
那么当我在 app.component 中更改 style 的属性时,如何更新 my-component 呢?这个绑定是不同的。
【问题讨论】:
-
你可以使用EventEmitter。
-
how can I refresh the my-component,您需要在MyComponent..中更新什么? -
@ganesh045 角度属性绑定触发子组件。当我更改属性绑定的子组件时,它会立即更改。但在这种情况下,我有一个复杂的对象。那么我该怎么做呢?
-
this stuation I have a complex object这与复杂对象或简单对象无关。您需要更新什么,是来自父组件的子组件变量还是来自子组件的父组件变量...?可能是我无法正确理解您的要求。你能详细说明一下吗... ;)