Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.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
Angular 如果是离子型,则与ng承诺_Angular_Ionic Framework_Promise - Fatal编程技术网

Angular 如果是离子型,则与ng承诺

Angular 如果是离子型,则与ng承诺,angular,ionic-framework,promise,Angular,Ionic Framework,Promise,下面是我的返回布尔承诺代码 BluetoothEnabled():任意{ this.print.isBluetoothPrinterEnabled()。然后( ()=>{return true}, ()=>{return false} ) } 这是我的带有ngif的离子按钮,它不工作 扫描 它进入无限循环..需要更改什么 编辑: 编辑: startScanning(){ this.pairedDevices=null; this.unpairedDevices=null; this.get

下面是我的返回布尔承诺代码

BluetoothEnabled():任意{
this.print.isBluetoothPrinterEnabled()。然后(
()=>{return true},
()=>{return false}
)
}
这是我的带有ngif的离子按钮,它不工作

扫描
它进入无限循环..需要更改什么

编辑:

编辑:

startScanning(){
this.pairedDevices=null;
this.unpairedDevices=null;
this.gettingDevices=true;
常数unPair=[];
this.print.dicoverBluetoothUnPairedPrinter()。然后((成功)=>{
success.forEach((值,键)=>{
var=false;
....
});
this.unpairedDevices=取消配对;
this.gettingDevices=false;
},
(错误)=>{
控制台日志(err);
});
this.print.searchBluetoothPrinter()。然后((成功)=>{
this.pairedDevices=成功;
},
(错误)=>{
控制台日志(err);
});
}

这是扫描设备的扫描功能。它将扫描配对和未配对的设备>>

您的代码存在多个问题

其中第一个
BluetoothEnabled()
不会返回布尔承诺。您需要添加
return
语句才能返回任何内容。如果您键入了函数的返回类型,编译器就会检测到这个问题

BluetoothEnabled():承诺{
**返回**this.print.isBluetoothPrinterEnabled()。然后(
()=>{return true},
()=>{return false}
);
}
其次,
*ngIf
正在检查函数返回的值,如果该值是一个承诺,它将始终为真

let test=新承诺(()=>false);
如果(测试)
控制台日志(“不正常”);
输出

Not okay
您需要通过使用来告诉Angular结果是异步的

扫描

请注意,您需要在模块中导入
CommonModule
,才能使用它。

“不工作”不是问题描述。会发生什么?为什么这是错误的?完整引用所有错误。谢谢@Macro..我是否必须更改BluetoothEnabled()函数?@Ajt对不起,我不明白你的问题。我的代码的第一个示例是对
BluetoothEnabled
函数的更正。@Macro我已更改了代码..但仍然存在相同的问题..BluetoothEnabled()循环添加了问题..但该代码在添加ngif之前..之后工作正常..如果我删除了nf if works fine,则很难判断,因为我无法调试您的代码。可能是一个变更检测问题。尝试设置一个类变量“isEnable”并赋值,这样您就可以在
*ngIf
中使用该变量,而不是调用函数。