使用字符串模型通过引用传递

时间:2017-02-15 00:36:49

标签: javascript angular

我遇到了为Angular 2中的函数传递String的问题。我想通过传入String数组,输出字符串和触发器创建一个可以交替和闪烁的通用函数动画的状态。但是,String总是按值传递。我如何解决它,以便它可以通过引用传递并更改组件中的模型?

import { 
  Component, OnInit,
  trigger, state, animate, transition, style 
} from '@angular/core';

@Component({
  selector: 'app-home',
  templateUrl: './home.component.html',
  animations: [
    trigger('visibilityChanged', [
      state('true' , style({ opacity: 1, transform: 'scale(1.0)' })),
      state('false', style({ opacity: 0, transform: 'scale(0.0)'  })),
      transition('1 => 0', animate('500ms')),
      transition('0 => 1', animate('900ms'))
    ])
  ]
})
export class HomeComponent implements OnInit {
  fancyText: {}
  isVisible: boolean = true
  fancyArr: Array<string>

  constructor() {
    this.fancyArr = ['Fancy', 'Text', 'Blinking', 'Alot']
    this.fancyText = this.fancyArr[0]
  }

  ngOnInit() {
    this.funcToTie(this.fancyArr, this.fancyText, this.isVisible)
  }

  funcToTie(textArr, outputText, stateAnimation){
    var curNewsIndex = 0
    setInterval(() => {
      ++curNewsIndex
      if (curNewsIndex >= textArr.length) {
        curNewsIndex = 0
      }
      stateAnimation = !stateAnimation
      setTimeout(() => {
        outputText = textArr[curNewsIndex]
        stateAnimation = !stateAnimation
      }, 500)
      console.log(outputText)
      console.log(this.fancyText)
    }, 2000);
  }
}

0 个答案:

没有答案