Angular2,在settimeout中变量更改后查看不更新

时间:2015-12-28 10:33:59

标签: javascript typescript angular angular2-template

我正在尝试将我的第一个angular2应用程序设置为实验并使用最新的beta版本。

我面临一个奇怪的问题,即我在视图中使用的变量在设置超时后没有更新。

@Component({
    selector: "my-app",
    bindings: []
})

@View({
    templateUrl: "templates/main.component.html",
    styleUrls: ['styles/out/components/main.component.css']
})

export class MainComponent {

    public test2 = "initial text";

    constructor() {
        setTimeout(() => {
            this.test2 = "updated text"; 
        }, 500);

    }
}

正如你所看到我有一个名为test2的变量,在构造函数中我设置了500毫秒的超时,我将值更新为"更新了文本"。

然后在我的视图main.component.html中我只使用:

{{ test2 }}

但是该值永远不会被设置为"更新文本"并保持"初始文本"永远即使更新部分被击中。如果我按照angular2教程,他们真的没有给我这个解决方案的答案。想知道是否有人会知道我在这里缺少什么。

编辑:我使用的完整代码包括bootstrap和html等

<html>
<head>
    <title>Angular 2</title>
    <script src="/node_modules/systemjs/dist/system.src.js"></script>
    <script src="/node_modules/reflect-metadata/reflect.js"></script>
    <script src="/node_modules/angular2/bundles/angular2.dev.js"></script>

    <script src="/node_modules/q/q.js"></script>
    <script src="/node_modules/jquery/dist/jquery.js"></script>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <script src="/bower_components/breeze-client/breeze.debug.js"></script>
    <script src="/bower_components/datajs/datajs.js"></script>
    <script src="/bower_components/bootstrap-less/js/collapse.js"></script>
    <script src="/bower_components/bootstrap-less/js/modal.js"></script>

    <script src="/bower_components/signalr/jquery.signalR.js"></script>
    <script src="http://localhost:64371/signalr/js"></script>

    <link href="styles/out/main.css" type="text/css" rel="stylesheet"  />
    <script>
        System.config({
            map: {
                rxjs: '/node_modules/rxjs' // added this map section
            },
            packages: {'scripts/out': {defaultExtension: 'js'}, 'rxjs': {defaultExtension: 'js'}}
        });

        System.import('scripts/out/main');

    </script>
</head>
<body>
    <my-app>loading...</my-app>
</body>
</html>

带引导程序的main.ts:

import {Component} from 'angular2/core';
import {bootstrap} from 'angular2/platform/browser'
import {COMMON_DIRECTIVES} from './constants';
import {MainComponent} from './components/main.component'
bootstrap(MainComponent);

主component.html

{{ test2 }}

4 个答案:

答案 0 :(得分:9)

正如Vlado所说,它应该有用; - )

我认为angular2-polyfills.js库应该包含在您的页面中。我看不出来。该文件本质上是zone.js和reflect-metadata的混搭。区域参与检测更新。

你可以看一下Bryan Ford解释它是什么的视频:https://www.youtube.com/watch?v=3IqtmUscE_U

希望它可以帮到你, 亨利

答案 1 :(得分:6)

那应该有用。你在控制台中有任何其他错误吗?

@Component({
 selector: 'my-app',
 template: `<h1>Hello {{title}}</h1>`
})
export class App {
 public title: string = "World";
 constructor() {
   setTimeout(() => {
    this.title = "Brave New World"
   }, 1000);)
 }
}

看看这个Plunker: http://plnkr.co/edit/XaL4GoqFd9aisOYIhuXq?p=preview

答案 2 :(得分:4)

我遇到了一个与OP非常相似的问题,即使在基本的Angular2设置中,对绑定属性的更改也不会自动反映在视图中。此时我们正在使用Angular2 2.0.0-rc.6。 没有错误消息。

最后,我发现罪魁祸首是es6-promise.js的引用,这是我们使用的第三方组件“必需”。不知怎的,这干扰了我们正在使用的core-js引用,这在一些Angular2教程中用rc6建议。

一旦我删除了es6-promise.js引用,在更改组件的属性后(通过Promise或timeout),视图就会正确更新。

希望有一天能帮到某人。

答案 3 :(得分:3)

在Angular2(~2.1.2)中,使其工作的另一种方法是通过ChangeDetectorRef类。原始问题代码如下所示:

import { 
  ChangeDetectorRef
  // ... other imports here
} from '@angular/core';

 @Component({
    selector: "my-app",
    bindings: []
})

@View({
    templateUrl: "templates/main.component.html",
    styleUrls: ['styles/out/components/main.component.css']
})

export class MainComponent {

    public test2 = "initial text";

    constructor(private cd: ChangeDetectorRef) {
        setTimeout(() => {
            this.test2 = "updated text"; 

            // as stated by the angular team: the following is required, otherwise the view will not be updated
            this.cd.markForCheck();

        }, 500);
    }
}