gpt4 book ai didi

javascript - 回调值中的 Angular 2 函数不更新 View

转载 作者:数据小太阳 更新时间:2023-10-29 04:53:43 25 4
gpt4 key购买 nike

我已经创建了函数和内部函数我在回调响应后调用一个回调函数我有更新字符串变量但是这个字符串变量没有更新我的 View 。

import { Component } from 'angular2/core'; 

@Component({
selector : "myview"
templateUrl : 'app/view/myview.component.html'
})


export class ViewComponent {

getAddress : string;
public totlaBalance : string;

getBalance():void{
var self = this;
getBalanceData(this.getAddress,function(error,res){
console.log(res);
self.totlaBalance = res;


});
}
}

在html文件中

<h1>Balance = {{totlaBalance}} </h1>

包.js

 "dependencies": {
"angular2": "2.0.0-beta.15",
"systemjs": "0.19.26",
"es6-shim": "^0.35.0",
"reflect-metadata": "0.1.2",
"rxjs": "5.0.0-beta.2",
"zone.js": "0.6.10",
"bootstrap": "^3.3.6"
},

在控制台中显示值但在 View 中值未更新。
我正在使用第 3 方回调函数,它不允许箭头函数。

最佳答案

您只需要使用箭头函数(()=>)ChangeDetectorRef,如下所示,

import {Injectable, ChangeDetectorRef } from 'angular2/core';  //<<<===here

export class ViewComponent {
getAddress : string;
public totlaBalance : string;

constructor(private ref: ChangeDetectorRef){} //<<<===here

getBalance():void{
var self = this;
getBalanceData(this.getAddress,(error,res)=>{ //<<<===here
console.log(res);
self.totlaBalance = res;
self.ref.detectChanges(); //<<<===here
});
}
}

关于javascript - 回调值中的 Angular 2 函数不更新 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39673030/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com