25

我正在尝试将我的第一个 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 教程,他们并没有真正给我这个解决方案的答案。想知道是否有人会知道我在这里缺少什么。

编辑:我正在使用的完整代码,包括引导程序和 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);

主要组件.html

{{ test2 }}
4

4 回答 4

9

正如弗拉多所说,它应该工作;-)

我认为该angular2-polyfills.js库应该包含在您的页面中。我看不到它。该文件本质上是 zone.js 和反射元数据的混搭。区域参与更新检测。

您可以观看这段视频,其中 Bryan Ford 解释了它是什么:https ://www.youtube.com/watch?v=3IqtmUscE_U 。

希望它可以帮助你,蒂埃里

于 2015-12-28T11:04:27.237 回答
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

于 2015-12-28T10:59:07.507 回答
4

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

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

一旦我摆脱了 es6-promise.js 引用,在更改我的组件上的属性(通过 Promise 或超时)后,视图会正确更新。

希望有一天这对某人有所帮助。

于 2016-09-09T08:43:53.163 回答
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);
    }
}
于 2017-01-30T17:39:26.703 回答