angular 2.0 强制刷新如$apply

angular 2.0 mandatory refresh like $apply

我正在创建一个应用程序来将组件(angular2 组件,下同)从列表中拖放到另一个组件中。在这个过程中我使用了 interactjs 来拖放组件。但是当我把它放到另一个组件中时,它只会在拖动的组件模板中加载静态 html。

例如我要将这个组件添加到另一个组件中

import {Component, View} from 'angular2/angular2'

@Component{
    selector:"sample"
}

@View{
    template:`<p>static content</p><p>{{contentToBind}}</p>`
}

class Sample{
    contentToBind:string= "I am the binding content."
}

添加后,应该如下所示

static content
I am the binding content.

但实际上它只是显示像

这样的静态内容
static content

你可以在这个plunker中查看这个例子。

And 然后我给这个Sample组件绑定一个click HostListener,当我点击这个组件已经被添加到另一个组件中时,会出现"I am binding content"(其他事件触发是一样的)。 我认为由于交互它已经跳出了angular2的生命周期。

angularJs1.x?

中与$apply()类似的方法

Interact 不是 angular 2 的一部分,也许您在 Interact 中的代码超出了您所说的 angular 2 范围。

您可以在 plunker 的 Interactjs 文件中使用 NgZone,我编辑了您的代码,现在可以使用了。你应该在 NgZone 中发送你的事件。

像这样。

myParent.ngZone.run(
    () => {
        console.log("onDrop...");
        ce.drop("Hello");
    }
);

在 plunker 上查看 Plunker