简体   繁体   English

如何在Angular中更改模型后更新视图?

[英]How to update the view after changing the model in Angular?

How do I let Angular propagate the changes i did to the model. 如何让Angular将我所做的更改传播到模型中。 In AngularJS this would be really easy, but i cannot seem to get it working in Angular. 在AngularJS中,这将非常简单,但我似乎无法让它在Angular中运行。 I know the entire change detection system and view propagation is changed entirely. 我知道整个变化检测系统和视图传播完全改变了。 Somehow, i need to inform angular of the changes. 不知何故,我需要通知角度变化。 But how can i do this in practise. 但是我怎么能在实践中做到这一点。

See this piece of typescript code: 看到这段打字稿代码:

import {Component, View, bootstrap, For} from 'angular2/angular2';

// Annotation section
@Component({
    selector: 'app'
})
@View({
    template: `
    <div *for="#user of users">
        {{user}}
    </div>
    `,
    directives: [For]
})
class App {
    users:Array<String>;

    constructor() {
        this.users = [];
        this.fillUsersAsync();
    }

    fillUsersAsync(){
        window['fetch']('http://jsonplaceholder.typicode.com/users')
            .then( resp => resp.json())
            .then( users => users.forEach(user => this.users.push(user.name)))
            .then( () => console.log('Retrieved users and put on the model: ', this.users));
    }
}

bootstrap(App);

You will see that, after the users get loaded into the model, the view doesn't update. 您将看到,在用户加载到模型后,视图不会更新。

I'm using systemjs 0.16, angular 2.0.0-alpha.23. 我正在使用systemjs 0.16,角度2.0.0-alpha.23。

See this plnkr for the example (currently, works only in chrome, as the new 'fetch' api is used) 请参阅此plnkr示例 (目前仅适用于chrome,因为使用了新的' fetch'api

I found the issue. 我发现了这个问题。 Apparently, its a bug to be fixed in alpha 27. See this bug report: https://github.com/angular/angular/issues/1913 显然,它是一个在alpha 27中修复的错误。请参阅此错误报告: https//github.com/angular/angular/issues/1913

Angular2 uses zonejs to know when to start the digest cycle (the dirty checking and update the view). Angular2使用zonejs知道何时开始摘要循环(脏检查和更新视图)。 As of now, zonejs is not triggered after fetching data with the new window.fetch(). 截至目前,在使用新的window.fetch()获取数据后,不会触发zonejs。

Replacing the window['fetch'] line to this fixed the issue for me: Zone.bindPromiseFn(window['fetch'])('http://jsonplaceholder.typicode.com/users') 将窗口['fetch']行替换为this修复了我的问题: Zone.bindPromiseFn(window['fetch'])('http://jsonplaceholder.typicode.com/users')

To update data at view in AngularJS2 you should use Forms . 要在AngularJS2中更新视图中的数据,您应该使用Forms You can read about this at this page or see more details here . 您可以在此页面阅读此内容或在此处查看更多详细信息。 If I understand Forms correctly, you should modify yout @View part like this: 如果我正确理解了表单,你应该修改你的@View部分,如下所示:

@View({
    template: `
    <div *for="#user of users" control="users">
        {{user}}
    </div>
    `,
    directives: [For]
})

--edited --edited

Try this: 试试这个:

import {Component, View, bootstrap, For} from 'angular2/angular2';

// Annotation section
@Component({
    selector: 'app'
})
@View({
    template: `
    <div [control]="users" *for="#user of users">
        {{user.value}}
    </div>
    `,
    directives: [For, forms]
})
class App {
    users:Array<String>;

    constructor() {
        this.users = new Control([]);
        this.fillUsersAsync();
    }

    fillUsersAsync(){
        window['fetch']('http://jsonplaceholder.typicode.com/users')
            .then( resp => resp.json())
            .then( users => 
                var usersArr = []
                users.forEach(user => 
                      usersArr.push(user.name))
                this.users = new Control(usersArr));
        }
 }

 bootstrap(App);

I am not sure that my answer is totally correct, but I can't find more information. 我不确定我的答案是否完全正确,但我找不到更多信息。 Experiment with this code and may the Force be with you! 尝试使用此代码,力量可能与您同在! :) :)

Also I found this link , very informative. 我也找到了这个链接非常有用。

As I understand, when in constructor you should initialize youre users variable via new Control(some data) and then, in view template you can access to this data like this - {{users.value}} . 据我所知,在构造函数中,您应该通过new Control(some data)初始化您的users变量,然后在视图模板中,您可以像这样访问这些数据 - {{users.value}}

If it does not work: try the same but with very simple data, eg use string instead of an array. 如果它不起作用:尝试相同但使用非常简单的数据,例如使用字符串而不是数组。

And this video will helps to unserstand forms in ng2. 此视频将有助于在ng2中展示表单。

Just answered it here Angular2 View Not Changing After Data Is Updated 刚刚在这里回答了Angular2视图在更新数据后没有更改

Basically, there was a bug in Beta 2.0.0-beta.1. 基本上,Beta 2.0.0-beta.1中存在一个错误。

Hope it helps! 希望能帮助到你!

Ng-prefixes are back. Ng前缀又回来了。 For has now become ngFor for Angular2 versions alpha-24+. For现在已成为ngFor版本alpha-24 +的ngFor。

import {NgFor} from 'angular2/directives';
@View({
  directives: [ngFor]
})

Then use *ng-for=#user of users" in the template. 然后在模板中使用*ng-for=#user of users"

Checkout the ngFor docs or a working example 查看ngFor文档工作示例

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

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