Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs Ng模型变量在$onInit方法中更改,但模型不';不变_Angularjs - Fatal编程技术网

Angularjs Ng模型变量在$onInit方法中更改,但模型不';不变

Angularjs Ng模型变量在$onInit方法中更改,但模型不';不变,angularjs,Angularjs,我有一个简单的货币转换器应用程序,在获得数据后,我设置了结果变量,该变量取决于输入值,但在从输入更改后,结果输入不会更改 $onInit() { this.currencyService.getData('http://api.fixer.io/latest').then((res) => { this.data = res this.values = Object.keys(this.data.rates) this.result = this.amoun

我有一个简单的货币转换器应用程序,在获得数据后,我设置了结果变量,该变量取决于输入值,但在从输入更改后,结果输入不会更改

  $onInit() {
  this.currencyService.getData('http://api.fixer.io/latest').then((res) => {
    this.data = res
    this.values = Object.keys(this.data.rates)
    this.result = this.amount * this.data.rates[this.fromVal] / this.data.rates[this.toVal]
    this.isLoad= true

  })

}
<div class="row">
  <div class="col-md-2">
    <label>Amout</label>
    <input type="number" class="form-control" ng-model='$ctrl.amount'>
  </div>
  <div class="col-md-2">
    <label>Result</label>
    <input type="number" class="form-control"  ng-model='$ctrl.result'>
  </div>

</div>
$onInit(){
此.currencyService.getData('http://api.fixer.io/latest)。然后((res)=>{
this.data=res
this.values=Object.keys(this.data.rates)
this.result=this.amount*this.data.rates[this.fromVal]/this.data.rates[this.toVal]
this.isLoad=true
})
}
数量
结果

原因在于:结果初始化为$onOnit,但“from”输入值稍后会更改。因此,当“from”值更改时,需要重新计算结果。使用ng更改。为什么要使用and input字段来显示组件的输出(结果)?我在$onInit中初始化了结果,但当输出值更改时它不应该更改吗?是的,我通过在第一次输入时放置
ng change='$ctrl.getResult()'
使此代码起作用,但我想知道为什么在金额更改时结果不更新没有,它不应该改变。您以$onInit计算结果。之后,result包含一个值,当其他变量发生变化时,它没有理由神奇地包含另一个值。感谢回复,我认为如果我用ng模型绑定result和amout,angular将设置2个观察者,因此当amount发生变化时,它将导致结果的变化