从 angular 控制器调用异步函数
call async function from angular controller
我想从 angular 表单模板控制器调用异步函数。但如果我这样做,执行就会挂起。
遵循 html 模板:
<div class="container">
<form>
<div class="form-group">
<label for="address">Identnummer (Adresse) *</label>
<input type="text" class="form-control" id="address" required placeholder="0x"
[(ngModel)]="model.address"
#address="ngForm"
[ngClass]="setAddressCssValidityClass(address)"
ngControl="address">
</div>
<div class="row">
<div class="col-auto">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
<small class="col text-right">* Required</small>
</div>
</form>
</div>
这是我的控制器的简化版本:
import 'package:angular/angular.dart';
import 'package:angular_forms/angular_forms.dart';
@Component(
selector: 'sCert-validate-form',
templateUrl: 'sCert_validate_form_component.html',
directives: [coreDirectives, formDirectives],
)
class ScertValidateFormComponent {
Map<String, bool> setAddressCssValidityClass(NgControl control) {
var validityClass;
//callCertificateByAddress(control.value);
if(control.value == "test"){
validityClass = 'is-valid';
callCertificateByAddress(control.value);
}
else{
validityClass = 'is-invalid';
}
Map<String, bool> map = {validityClass: true};
return map;
}
Future<bool> callCertificateByAddress(String address) async {
// some code -> also does not work if removed
return true;
}
}
Future<bool> callCertificateByAddress(String address) async
必须是异步的,因为我在那里进行异步库调用。但是如何结合模板实现呢?
像这样使用异步管道
[ngClass]="setAddressCssValidityClass(address) | async"
存在问题:您将回调绑定到此处的输入 [ngClass]="setAddressCssValidityClass(address)"
angular 在每个更改检测周期调用您的函数 [调用网络]。来自 http 的响应强制进行新的更改检测,它一次又一次地递归调用 http。将结果保存到某个变量并将其绑定到 ngClass 而不是方法
ngOnInit() {
this.classes = this.setAddressCssValidityClass(address);
}
....
[ngClass]="classes"
我想从 angular 表单模板控制器调用异步函数。但如果我这样做,执行就会挂起。
遵循 html 模板:
<div class="container">
<form>
<div class="form-group">
<label for="address">Identnummer (Adresse) *</label>
<input type="text" class="form-control" id="address" required placeholder="0x"
[(ngModel)]="model.address"
#address="ngForm"
[ngClass]="setAddressCssValidityClass(address)"
ngControl="address">
</div>
<div class="row">
<div class="col-auto">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
<small class="col text-right">* Required</small>
</div>
</form>
</div>
这是我的控制器的简化版本:
import 'package:angular/angular.dart';
import 'package:angular_forms/angular_forms.dart';
@Component(
selector: 'sCert-validate-form',
templateUrl: 'sCert_validate_form_component.html',
directives: [coreDirectives, formDirectives],
)
class ScertValidateFormComponent {
Map<String, bool> setAddressCssValidityClass(NgControl control) {
var validityClass;
//callCertificateByAddress(control.value);
if(control.value == "test"){
validityClass = 'is-valid';
callCertificateByAddress(control.value);
}
else{
validityClass = 'is-invalid';
}
Map<String, bool> map = {validityClass: true};
return map;
}
Future<bool> callCertificateByAddress(String address) async {
// some code -> also does not work if removed
return true;
}
}
Future<bool> callCertificateByAddress(String address) async
必须是异步的,因为我在那里进行异步库调用。但是如何结合模板实现呢?
像这样使用异步管道
[ngClass]="setAddressCssValidityClass(address) | async"
存在问题:您将回调绑定到此处的输入 [ngClass]="setAddressCssValidityClass(address)"
angular 在每个更改检测周期调用您的函数 [调用网络]。来自 http 的响应强制进行新的更改检测,它一次又一次地递归调用 http。将结果保存到某个变量并将其绑定到 ngClass 而不是方法
ngOnInit() {
this.classes = this.setAddressCssValidityClass(address);
}
....
[ngClass]="classes"