简体   繁体   English

如何将参数从组件传递到 angular 中的 Class

[英]How to pass a parameter from Component to a Class in angular

I want to pass the parameter ColorPicker (Color1) from object-settings.component.html to poiComponentClass.ts, to assign it to the color of the cylinder ( const material = new THREE.MeshBasicMaterial( {color: 0xFF00FF } ); )我想将参数 ColorPicker (Color1) 从 object-settings.component.html 传递给 poiComponentClass.ts,以将其分配给圆柱体的颜色 ( const material = new THREE.MeshBasicMaterial( {color: 0xFF00FF } ); )

object-settings.component.html对象设置.component.html

<div class="col-md-5">
      <input [style.background]="color1" [(colorPicker)]="color1" (colorPickerOpen)="onEventLog('colorPickerOpen', $event)" (colorPickerClose)="onEventLog('colorPickerClose', $event)" (cpInputChange)="onEventLog('cpInputChange', $event)" (cpSliderDragStart)="onEventLog('cpSliderDragStart', $event)" (cpSliderDragEnd)="onEventLog('cpSliderDragEnd', $event)"/>
    </div>

object-settings.component.ts对象设置.component.ts

 public onChangeColorCmyk(color: string): Cmyk
 {
 const hsva = this.cpService.stringToHsva(color);
 if (hsva) {
 const rgba = this.cpService.hsvaToRgba(hsva);
 return this.cpService.rgbaToCmyk(rgba);
 }
 return new Cmyk(0, 0, 0, 0);
 } 
 public onChangeColorHex8(color: string): string {
 const hsva = this.cpService.stringToHsva(color, true);
 if (hsva) {
 return this.cpService.outputFormat(hsva, 'rgba', null);}
 return '';}

poiComponentClass.ts poiComponentClass.ts

const THREE = this.context.three;
const textureLoader = new THREE.TextureLoader();
const geometry = new THREE.CylinderGeometry( 0.09, 0.09, 5, 5 );
geometry.rotateZ(-Math.PI * 0.5);
const material = new THREE.MeshBasicMaterial( {color: 0xFF00FF } );
const cylinder = new THREE.Mesh( geometry, material );
if (!this.inputs.sprite) {
return;} 
if (!this.inputs.sprite.startsWith('/assets/')) {
textureLoader.crossOrigin = 'Anonymous';}
let map;
if (this.inputs.sprite.indexOf('.gif') !== -1) {
map = new GifLoader().load(this.inputs.sprite);
 } else {
map = textureLoader.load(this.inputs.sprite);}
map.minFilter = THREE.LinearFilter;
map.wrapS = map.wrapT = THREE.ClampToEdgeWrapping;
this.material = new THREE.SpriteMaterial( { map, color: this.inputs.color, fog: false );
this.material.alphaTest = 0.5;
this.material.map.encoding = THREE.sRGBEncoding;
this.sprite = new THREE.Sprite( this.material );
this.sprite.add(cylinder);
this.onObjectReady(this.sprite, true);}

There is a lot missing here.这里缺少很多东西。 We do not know the second snippet is inside a class decorated with @Component or not for example.例如,我们不知道第二个片段是否在用@Component装饰的 class 内。 There is also a lot not relevant to the question as well.也有很多与问题无关的内容。 If you need to learn about components communication in Angular you can start with the Official Angular Documentation (Component Interaction).如果您需要了解 Angular 中的组件通信,您可以从Angular 官方文档(组件交互)开始。

You can use different strategies to achieve your goal, such as using a service (an injectable class) or using Decorated Properties with Event Emitters or even using Facades or state-management libraries .您可以使用不同的策略来实现您的目标,例如使用service (可注入类)或使用带有Event EmittersDecorated Properties ,甚至使用Facadesstate-management libraries The options are manay.选项很多。

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

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