简体   繁体   English

后端服务中阵列的Angular2更新视图

[英]Angular2 Update View from Array in Backend Service

I have a ListComponent and EntryComponent for appending an item into the list. 我有一个ListComponent和EntryComponent用于将一个项目追加到列表中。 When an item is pushed into the list through the DataService, the list is not updated in the ListComponent. 通过DataService将项目推入列表时,该列表不会在ListComponent中更新。 Any ideas for the angular2 way of working about this? 关于angular2的解决方法有什么想法吗?

List Service: 清单服务:

import {Injectable} from "angular2/core";
@Injectable()
export class DataService{
    public items:Array<any> = [];
    addItem(item:string):void{
            this.items.push(item);
            console.log(this.items);
    }
}

Simple List Component: 简单列表组件:

import { Component, Input, Output } from 'angular2/core';
import {ChangeDetectionStrategy} from "angular2/core";
@Component({
    selector: "list",
    template: "<div><ul><li *ngFor='#item of items'>{{item}}</li></ul>",
    changeDetection:ChangeDetectionStrategy.OnPush
})
export class ListComponent{
    @Input() items:Array<any>;
}

Entry Component: 条目组件:

import {Component,EventEmitter, Output} from "angular2/core";

@Component({
    selector:'entry-form',
    template:'<div><input type="text" #myinput (keyup.enter)="emitEntry(myinput)"/></div>'
})
export class EntryComponent{
    @Output() newEntry = new EventEmitter();
    emitEntry(input):void{
        this.newEntry.emit(input.value);
        input.value="";
    }
}

App Component (root component): 应用程序组件(根组​​件):

import { Component } from 'angular2/core';
import { ListComponent } from './list.component';
import {DataService} from './list-service';
import {EntryComponent} from './entry-form';

@Component({
    selector:'my-app',
    directives:[ListComponent,EntryComponent],
    providers:[DataService],
    template:
        "<list [items]='listService.items' ></list> " +
        "<entry-form (newEntry)='listService.addItem($event)'></entry-form> "
})

export class AppComponent{
    constructor(public listService: DataService){
    }
}

Edit I was able to produce the correct result by adding this line to the addItem() method in the DataService: 编辑通过将以下行添加到DataService中的addItem()方法中,我能够产生正确的结果:

this.items = this.items.splice(0,this.items.length-1);

However, this doesn't seem the angular2 way. 但是,这似乎不是angular2方式。

Thanks in advance! 提前致谢!

Edit Here is a plunker of the code 编辑这是代码的一大堆

Remove the ´changeDetection:ChangeDetectionStrategy.OnPush´ 删除“ changeDetection:ChangeDetectionStrategy.OnPush”

Just leave the component to: 只需将组件放在:

@Component({
    selector: "list",
    template: "<div><ul><li *ngFor='#item of items'>{{item}}</li></ul>"
})

You can see it here 你可以在这里看到

With ChangeDetectionStrategy.OnPush , Angular will only check your component's databindings (such as {{item}} in your template) if at least one of the component's input properties has changed. 使用ChangeDetectionStrategy.OnPush ,Angular将仅在组件的输入属性中的至少一个发生更改的情况下检查组件的数据绑定(例如模板中的{{item}} )。 The other important piece of information is that, for input properties that are arrays, Angular change detection checks the arrays for reference changes only – ie, it only checks to see if array references have changed, not the contents of the arrays. 另一个重要的信息是,对于作为数组的输入属性,Angular更改检测仅检查数组以进行引用更改–即,它仅检查数组引用是否已更改,而不查看数组的内容。

Since array input property items continues to point to (reference) the same array when you push() new items from the server onto it, Angular change detection does not consider the array to have changed, hence no input property changes for Listcomponent are detected, so it doesn't check the component's databindings. 由于当您从服务器Listcomponent新项push()新数组时,数组输入属性items继续指向(引用)同一数组,因此角度更改检测不会认为该数组已更改,因此未检测到Listcomponent输入属性更改,因此它不会检查组件的数据绑定。

Removing ChangeDetectionStrategy.OnPush works because by default, Angular change detection checks all databindings, even if the input properties have not changed. 删除ChangeDetectionStrategy.OnPush之所以起作用,是因为默认情况下,即使输入属性未更改,Angular更改检测也会检查所有数据绑定。 Since you have bindings for each element of the array, change detection will notice that there is a new binding when ngFor is re-evaluated. 由于您对数组的每个元素都有绑定,因此更改检测将注意到在重新评估ngFor时,存在一个新的绑定。

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

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