我需要在 angular2 中添加annyang库。谁能建议我如何使用 system.js 和 typescript 添加?我们有一个 angular 1服务。
我创建了一个 annyang 服务,代码是
import {Injectable} from '@angular/core'
declare let annyang:any
@Injectable()
export class AnnyangService {
start() {
annyang.addCommands(this.commands);
annyang.start({continuous: false });
}
commands = {};
}
组件代码
import { Component,EventEmitter } from '@angular/core'
import {EventsListComponent} from './events/events-list.components'
import {NavBarComponent} from './nav/navbar.component'
import {AnnyangService} from './common/annyang.service'
declare var annyang:any
@Component({
selector: 'events-app',
template: `
<h3>{{iv}}</h3>
<button class="btn btn-primary" (click) = "clickEvent()">click</button>
})
export class EventsAppComponent{
iv:any
nameArray:Array<string> = ["a","p","n"]
constructor(private annyang:AnnyangService){
}
ngOnInit(){
this.iv="asdf"
console.log(this.iv)
this.annyang.commands = {
'*val': (val)=>{
console.log("command start")
this.updateVar(val)
}
}
this.annyang.start();
}
clickEvent = function(){
let abc = this.nameArray[Math.floor(Math.random() * (this.nameArray.length)) + 0]
this.updateVar(abc)
console.log(abc)
}
updateVar = function(val){
console.log(this.iv)
this.iv = val
console.log(this.iv)
}
}
单击按钮时,我可以更改“iv”,但使用 annyang 虽然使用相同的 updateVar 并没有获得更新的值,它也反映在控制台中。我需要任何事件方法来代替 ngOnInit 吗?或者我调用服务的方式不正确?