[英]angular js ng-if is not getting return value from async function
我正在評估ng中的功能,是否知道用戶是否有權執行操作。 問題在於,要求值的函數會調用后端,並且顯然在ng-if旁邊無法正常工作,因為它打印的是空對象,而不是函數返回的結果。
我將結果顯示到html中,以了解該函數正在響應什么:
{{organizationController.isAllowedToInvite ()}}
此打印給我一個空對象{}
這是我正在評估的功能:
self.isAllowedToInvite = () => {
organizationRoleService.getUserPermissions(self.organizationId)
.then((response) => {
const userPermissions = response.data;
if (userPermissions && userPermissions.organizationPermissions >= 4){
return true;
}
return false;
}, (error) => {
console.log(error);
});
};
這返回的是對還是錯,這本身就可以很好地工作。
在html中打印時,為什么不能顯示函數的結果? 因為沒有顯示true / false,而是顯示了一個空對象{}?
我希望它顯示真假,因此如果可以,我可以在ng中對其進行評估。
這是我的ng-if,顯然這不起作用,因為它具有{}值。
ng-if="organizationController.isAllowedToInvite()"
您的方法中有一個異步操作,您不能靜態檢查if中的條件。 您應該在控制器實例化上調用該方法,然后使用設置為true的標志(如果滿足所有條件)並檢查模板中的標志
self.isAllowedToInvite = () => {
self.isAllowed = false;
organizationRoleService.getUserPermissions(self.organizationId)
.then((response) => {
const userPermissions = response.data;
if (userPermissions && userPermissions.organizationPermissions >= 4){
self.isAllowed = true;
}
}, (error) => {
console.log(error);
});
};
self.isAllowedToInvite();
並在您的模板中
ng-if="organizationController.isAllowed"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.