简体   繁体   English

与作为输入传递给Angular2组件的对象进行交互

[英]Interact with object passed to Angular2 component as Input

(to save some reading -- the problem boiled down to a missing 'this' when referencing the @Input'ed variables) (以节省一些阅读时间-问题归结为在引用@ Input'ed变量时缺少的“ this”)

I have a parent class - this will (eventually) display a list of "QuestionComponents". 我有一个父类-这将(最终)显示“ QuestionComponents”的列表。

The relevant part of the parent class template is as follows: 父类模板的相关部分如下:

<question [(justText)]="testText"  [(justObj)]="testObj" [(myQuestion)]="singleQuestion"></question>

Suffice it to say I'm getting the "singleQuestion" object from a http service call. 可以说我从http服务调用中获得了“ singleQuestion”对象。 the Question class, of which "singleQuestion" is an instance, has a basic "toString" method for easier output debugging. Question类(其中的“ singleQuestion”是一个实例)具有基本的“ toString”方法,以简化输出调试。

testText, textObj, and singleQuestion are all just objects of increasing complexity for me as I tested. 在我测试时,testText,textObj和singleQuestion只是对我而言越来越复杂的对象。 For now it's very basic and only has one "question" as I work on my understanding. 现在,这是非常基本的,在我努力理解时只有一个“问题”。

The child "QuestionComponent" looks like this: 子级“ QuestionComponent”如下所示:

@Component ({
    selector:
        'question',
    templateUrl:
        './app/components/question/question.component.html',
    directives: [FORM_DIRECTIVES],

})


export class QuestionComponent {

    @Input() myQuestion:USGSQuestion
    @Input() justText:string
    @Input() justObj:object

     constructor() {

    }

    onClick() {
        myQuestion.generalInfo.label = "changed";
        console.log("change attempted");
    }
}

Note that as time moves on, I'll need to be able to do some heavy interaction with the objects that are passed into the QuestionComponent. 请注意,随着时间的流逝,我将需要能够与传递到QuestionComponent中的对象进行大量交互。 In fact, my preference was to just pass the Question object into the constructor, but passing data into the component constructor doesn't seem to work for some reason. 实际上,我的偏好是仅将Question对象传递到构造函数中,但是由于某些原因,似乎无法将数据传递到组件构造函数中。 (I digress) To attempt to check how I can interact with passed data , I built the onClick button and tried to change something in one of the @Input'ed variables. (我离题)为了尝试检查如何与传递的数据进行交互,我构建了onClick按钮并尝试更改@Input变量之一。

The template for the child object is as so: 子对象的模板如下:

<div *ngIf="!justText">
    no text passed
</div>
<div *ngIf="justText">
    <b>Here is the passed Text:</b><br>
     {{justText}} <br>
</div>
<br>
<div *ngIf="!justObj">
    no obj passed
</div>
<div *ngIf="justObj">
    <b>Here is the passed JSON:</b><br>
     Foo: {{justObj.foo}} <br>
     Baz: {{justObj.baz}}
</div>
<br>
<div class="question">
    <i>I am a question spot</i>

    <div *ngIf="!myQuestion">
        Loading Question...
    </div>
    <div *ngIf="myQuestion">
        <b>Here is the passed question:</b><br>
         {{myQuestion}} <br>
    </div>
</div>

<button (click)="onClick()">Clickit</button>

How do I get a reference to the @Input'ed objects inside the class? 我如何获得对类内部@ Input'ed对象的引用? (in this case, how would I modify "myQuestion" in the 'onClick()' function? ... and, secondarily, how would I ensure changes to the objects got passed to the view and updated? (在这种情况下,我将如何在“ onClick()”函数中修改“ myQuestion”?...,其次,我如何确保将对对象的更改传递给视图并进行更新?


I should note I already tried to pass the value from the 'view' back through the onClick call, like so: (change button to:) 我应该注意,我已经尝试过通过onClick调用将“视图”中的值传递回去,例如:(将按钮更改为:)

<button (click)="onClick(myQuestion)">Clickit</button>

(and change onClick to:) (并将onClick更改为:)

onClick(q) { q.generalInfo.label = "changed"; }

This did not work. 这没有用。

I'm an idiot. 我是个白痴。

After a few hours of research and searching (Before asking) it all came down to adding "this". 经过几个小时的研究和搜索(询问之前),所有这些都归结为添加“ this”。

... as in "myQuestion.generalInfo.label = "changed";" ...如"myQuestion.generalInfo.label = "changed";" should have been "this.myQuestion.generalInfo.label = "changed";" 应该是"this.myQuestion.generalInfo.label = "changed";"

Some days you gotta love programming, eh? 有时候你会喜欢编程的,是吗?

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

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