【问题标题】:Angular2 subscribing to changes to @Input in Child ComponentAngular2订阅对子组件中@Input的更改
【发布时间】:2016-11-23 01:22:42
【问题描述】:

我有一个父子组件。父组件具有index,并将其作为@Input 传递给子组件。这个index 值在父组件中不断变化,在我的子组件中,我想在每次父组件更改index 时运行一个函数。这有一个不断改变它的幻灯片组件。我怎样才能做到这一点?我已尝试使用以下代码 (this.index.subscribe(() =>),但每次尝试初始化订阅时,它都不是一个函数。

编辑:Child 模板中没有可能影响此的相关代码,因此未提供。 ngOnChange 似乎不起作用,因为指令中发生了更改,而不是 parent 的模板。

孩子:

import {Component, OnInit, ViewChild, Input} from '@angular/core';
import {Observable} from 'rxjs/Observable';

@Component({
    selector: "child",
    templateUrl: "components/child/child.html",
})

export class ChildComponent implements OnInit {
    @Input() index: string;
    currentIndex: any;

    constructor() {}

    ngOnInit() {}

    ngOnChanges(){}

    ngAfterViewInit() {
        console.log("index " + this.index);
        this.currentIndex = this.index.subscribe(() => {
             console.log("index " + this.index);
        })
    }

    ngOnDestroy() {
        this.currentIndex.unsubscribe();
    }

}

家长:

import {Component, ElementRef, OnInit, ViewChild} from '@angular/core';
import {Page} from "ui/page";
import {ChildComponent} from '/components/child/child.component'

@Component({
    selector: "parent",
    template: "<child [index]="index"></child>",
    directives: [ChildComponent]
})

export class ParentComponent implements OnInit {

    index: string = "0,1";

    constructor(private page: Page) {
    }



}

【问题讨论】:

  • 首先,ParentComponent 的模板应该是 template,而不是 templateUrl。另外,你能把ChildComponent 的模板展示一下吗?
  • index in ParentComponent string 并且 ChildComponent 期望它是 Observable&lt;any&gt;。你就是这样使用它们的吗?
  • 是的,否则你会得到Error: this.index.subscribe is not a function
  • 请提供您的template的代码
  • this.slides 来自哪里?从您的代码中,它不能是 undefined 以外的任何东西(它没有在任何地方设置),这会在 let SlidesXml = this.slides.nativeElement; 处引发错误。

标签: angular


【解决方案1】:

https://angular.io/docs/ts/latest/api/core/index/Input-var.html

引用:

Angular 在更改期间自动更新数据绑定属性 检测。

如果你需要对输入做一些处理,看看get和set。 https://angular.io/docs/ts/latest/cookbook/component-communication.html#!#parent-to-child-setter

从文档中,这是一个示例。

import { Component, Input } from '@angular/core';
@Component({
  selector: 'name-child',
  template: `
    <h3>"{{name}}"</h3>
  `
})
export class NameChildComponent {
  _name: string = '<no name set>';
  @Input()
  set name(name: string) {
    this._name = (name && name.trim()) || '<no name set>';
  }
  get name() { return this._name; }
}

你不需要使用 observable。

【讨论】:

  • 有时您想在任何输入更改后重新触发组件的方法...然后使用ngOnChanges() 比在每个输入的每个设置器中运行该方法要简单得多,我猜。
  • 对于那些觉得 setter 技术很乏味的人,请查看 subjectize,它将 setter 封装在底层,您不需要私有变量。
【解决方案2】:

正如另一个答案中提到的,您可以使用ngOnChanges life hook。来自 Angular 的documentation

ngOnChanges:在 Angular 设置数据绑定输入属性后响应。 该方法接收当前值和先前值的更改对象。

查看下面的示例,了解如何使用它:

import { Component, Input, OnChanges  } from '@angular/core';

@Component({
  selector: 'child',
  template: `
    Value:
    <span>{{ value }}</span>
    <br />
    <br />
    Number of changes: {{ numberOfChanges }}
  `
})
export class ChildComponent implements OnChanges {
  @Input() value: any;

  private numberOfChanges: number = 0;

  ngOnChanges() {
    this.numberOfChanges++;
  }
}

Plunker:http://plnkr.co/edit/XjD1jjCnqiFdbhpTibIe

【讨论】:

  • 更新index的指令是什么意思?
  • 请检查SlidesXml.on("start", (event) =&gt; { ... }事件中的上下文
  • @bnussey:我已经修改了 Alexandre Junges 的 plunker 并对其进行了分叉,这个 plunker 表明即使更改指令中的代码也会更新孩子。
【解决方案3】:

ngOnChange 在每次输入参数更改时被调用。你甚至在你的 Child 组件中有它,但它没有正确实现:

ngOnChanges(changes: {[propertyName: string]: SimpleChange}) {
  // check the object "changes" for new data
}

更多详情:https://angular.io/docs/ts/latest/guide/lifecycle-hooks.html#!#onchanges

更新:父组件中的indexstring。由于某种原因,它在子组件中变成了Observable

【讨论】:

【解决方案4】:

将变化的值“转换”为可观察的另一种解决方案应该是

@Input() status: Status;
private status$: Subject<Status>;

constructor() {
    this.status$ = new Subject<Status>();
}

ngOnChanges(changes: {[propertyName: string]: SimpleChange}) {

    this.status.next(this.status);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-11-17
    • 1970-01-01
    • 2018-03-15
    • 2016-06-03
    • 2017-02-14
    • 2017-10-13
    • 2017-09-08
    • 2017-04-09
    相关资源
    最近更新 更多