首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >按引用的组件输入

按引用的组件输入
EN

Stack Overflow用户
提问于 2016-05-21 18:35:31
回答 2查看 358关注 0票数 0

我正在编写一个angular2组件。在这个组件中,我有3个输入。我将一个变量粘贴到每个输入。可以通过引用粘贴输入吗?

我希望能够将变量粘贴到组件中,一旦我更改了它的值,我希望更改反映在该组件外部的变量上。这意味着通过引用传递变量。在angular 2中有可能吗?我知道我可以订阅活动。并在更改时触发一个事件。我想知道是否有更简单的解决方案。

如有任何有关此问题的信息,将不胜感激。

更新

这是我创建的组件的代码。

代码语言:javascript
复制
import { Component } from '@angular/core';
import {MdButton} from '@angular2-material/button';
import {ImageRotation} from './image-rotation';
import {FileReaderEvent} from './file-reader-event';
@Component({
selector: 'image-upload',
templateUrl: '/client/imports/components/image-upload/image-upload.html',
directives: [MdButton],
inputs: ['imageUrl','imageFile','imageRotateDegrees']
})
export class ImageUploadComponent {
imageRotationObj:ImageRotation;
imageUrl:string;
imageSrc:any;
imageFile: any;
imageRotateDegrees:number;
imageExifRotation:number;
isImageFileExif:boolean;

imageChanged() {
    var reader = new FileReader();
    reader.onload =  (e:FileReaderEvent)=> {
        var exif = EXIF.readFromBinaryFile(e.target.result);
        switch (exif.Orientation) {
            case 8:
                    this.imageRotateDegrees=-90;
                    this.imageExifRotation=-90;
                    this.isImageFileExif=true;
                this.imageRotationObj.setImageRotation(-90);
                break;
            case 3:
                    this.imageRotateDegrees=180;
                    this.imageExifRotation=180;
                    this.isImageFileExif=true;
                this.imageRotationObj.setImageRotation(180);
                break;
            case 6:
                    this.isImageFileExif=true;
                    this.imageExifRotation=90;
                    this.imageRotateDegrees=90;
                this.imageRotationObj.setImageRotation(90);
                break;
            default:
                    this.isImageFileExif=false;
        }
    };
    var reader2 = new FileReader();
    reader2.onload = (e:FileReaderEvent)=> {
        this.imageSrc=e.target.result;
    }
    reader.readAsArrayBuffer(this.imageFile);
    reader2.readAsDataURL(this.imageFile);
}

constructor() {
    this.imageRotationObj=new ImageRotation();
 }

fileOnChange(event:any) {
    this.imageFile = event.target.files[0];
    this.imageChanged();
}
}

如你所见,我定义了3个输入。现在,我以以下方式使用该组件:

代码语言:javascript
复制
<image-upload [imageUrl]="imageUrl" [imageFile]="imageFile" [imageRotateDegrees]="imageRotateDegrees"></image-upload>

在这里,我将3个变量传递给组件。

并且组件从输入定义中理解这些变量。

现在.。我想要的是能够更改组件内部的变量,并且它们将在组件外部更改。据我所知,变量是通过值粘贴的,而不是通过引用。

那我该怎么办呢?

EN

回答 2

Stack Overflow用户

发布于 2016-08-24 23:01:20

我知道这已经是几个月后的事了,但如果你还想做这样的事情,你可以使用“@angular/ ViewChild”中的核心。ViewChild是父组件直接访问子变量/函数的一种方式,因此您可以在父组件中创建类似以下内容:

代码语言:javascript
复制
import [Component, ViewChild, AfterViewInit] from '@angular/core'

export class ParentComponent {
  @ViewChild(ChildComponent) childComponent: ChildComponent
  parentVariable

  ngAfterViewInit() {
    this.childComponent.childVariable = this.parentVariable
  }
}
票数 1
EN

Stack Overflow用户

发布于 2018-02-28 20:28:27

我不能完全确定,但双向数据绑定不是您正在寻找的吗?

看看这里:https://blog.thoughtram.io/angular/2016/10/13/two-way-data-binding-in-angular-2.html

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37361770

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档