【问题标题】:In angular2, how to make child image changes when parent image changes?angular2中,当父图发生变化时,如何使子图发生变化?
【发布时间】:2016-02-23 06:57:13
【问题描述】:

例如,我们有两张图片,一张在父组件中,另一张在子组件中。单击子图像可以使父图像发生变化,易于实现。但是单击父图像时如何更改子图像?

【问题讨论】:

    标签: events angular


    【解决方案1】:

    我会看到几种方法来做到这一点:

    【讨论】:

    • 谢谢蒂埃里。对不起,我问错了问题。我知道如何在使用输出单击子图像时更改父图像。我的问题实际上是:单击父图像时如何更改子图像?我试过使用输出,它没有用。
    • 我认为@Query 可以帮助您从父组件中引用子图像。这样您就可以在单击父项时对其进行更新。
    • 查看此链接了解更多详情:angular.io/docs/ts/latest/api/core/Query-var.html
    【解决方案2】:

    您的 plunker 无法正常工作,因此很难确定您的具体问题出在哪里。但一般情况并不太难。

    如果您想在单击子项时更改父项,只需在子项上使用output。例如 - 在此处单击子级会交替显示父级中的图像 (working plunk)

    import {Component, Output, EventEmitter} from 'angular2/core'
    
    
    @Component({
      selector: "child";
      template:`
        <div>
         <h3>  Child Image: </h3>
          <img src="{{uri}}" (click)="imageClicked($event)" />
          </div>
      `
    })
    export class childComponent {
      @Output() wasClicked: EventEmitter<Node> = new EventEmitter();
      uri: string = "https://upload.wikimedia.org/wikipedia/commons/e/e5/Элемент_И-НЕ_%28100%29.PNG"
    
      imageClicked(e) {
        console.log("child component recieved click" )
        this.wasClicked.emit(e)
      }
    }
    
    @Component({
      selector: "parent";
      template:`
        <div>
          <h2> Parent Image: </h2>
          <img src="{{uri}}" />
         </div>
        <child (wasClicked)="childClicked(e)"></child>
      `,
      directives: [childComponent]
    })
    export class parentComponent {
      switch: boolean = false;
      uri: string = "https://upload.wikimedia.org/wikipedia/commons/1/14/Элемент_Исключающее_ИЛИ_%28100%29.png"
    
      childClicked(e){
        console.log("parent recieved click event from child")
        this.switch
        ? this.uri = "https://upload.wikimedia.org/wikipedia/commons/1/14/Элемент_Исключающее_ИЛИ_%28100%29.png"
        : this.uri = "https://upload.wikimedia.org/wikipedia/commons/7/7a/Элемент_ИЛИ-НЕ_%28100%29.PNG"
        this.switch = !this.switch
    
      }
    }
    

    【讨论】:

      【解决方案3】:

      点击父图时如何改变子图?

      一般来说,当父母想要与孩子交流某些内容时,请使用子输入属性。如果要执行一些逻辑,请实现ngOnChanges(),以便在输入属性更改时收到通知:

      ngOnChanges(changes) {
        console.log(changes);
      }
      

      我假设您想在输入属性playBtnStatus 更改时在 PauseBtnComponent 中执行某些操作:

      ngOnChanges(changes) {
        console.log(changes);
        if(changes.playBtnStatus.currentValue) { ... }
        else { ... }
      }
      

      Plunker


      另一种选择是在父组件中使用@ViewChild@ViewChildren@Query 来获取对子组件(ren)的引用。然后你可以调用孩子的公共方法。

      【讨论】:

      • 谢谢。我稍微更改了代码以更改子图像,但它不起作用。 ngOnChanges(changes) { console.log(changes.playBtnStatus.currentValue) this.pauseEnabledImg = this.pauseEnabledImg; }
      • @J.Fun,你的评论没有意义:this.pauseEnabledImg = this.pauseEnabledImg;。您需要检查changes.playBtnStatus.currentValue 的值,并根据该值有条件地更改您的图像。
      • 对不起,我不是很清楚。我假设鼠标单击父图像后,它的状态应该从启用更改为禁用,然后它会调用 onChange 函数并让子图像启用。但是它并没有像我预期的那样工作。你能告诉我在 plunker 上吗? (父图像和子图像的状态总是相反的,一个可以触发另一个的状态,反之亦然)
      • @J.Fun,我更新了 plunker 以包含 if(!changes.playBtnStatus.currentValue) { this.currentPauseImg = this.pauseEnabledImg; }。看看这是不是你需要的。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-26
      • 2022-07-29
      • 1970-01-01
      • 1970-01-01
      • 2021-09-12
      相关资源
      最近更新 更多