在Angular 2 TS组件中重新使用knockoutJS 2组件

时间:2016-01-05 21:25:26

标签: angular knockout-2.0

我正在将项目迁移到以前用KnockoutJS编写的Angular2 w / Typescript。事实证明这很麻烦,所以我想简单地重复使用我在Angular2中使用KO编写的一些组件,但是我很难找到一个如果可行的例子。

我的代码如下所示:

myKOComponent.js

returns {viewModel: myViewModel, template:myTemplate}

myAngularComponent.ts

//Here I want to import myKOComponent which there are 2 things I am unsure about
  1. 如何导入组件,然后引用该对象返回。

  2. 我一直在使用的模式是父模式(即myAngularComponent)将模板附加到元素然后调用ko.applyBinding(new myViewModel,myTemplate),这可能吗?

  3. 谢谢你的时间!

1 个答案:

答案 0 :(得分:0)

这不是我实际尝试过的东西,但也许这样的东西会起作用

import {Component, OnInit} from 'angular/core';
import {MyViewModel} from './some-location';

declare var ko:any;

@Component{...}

export class MyComponent implements OnInit {

    ngOnInit() {
        ko.applyBindings(new MyViewModel());
    }
}