[英]angular js ng-if is not getting return value from async function
I am evaluating a function in a ng if to know if the user has permission to perform an action. 我正在评估ng中的功能,是否知道用户是否有权执行操作。 The problem is that the function to evaluate makes a call to the backend, and apparently does not work correctly next to the ng-if, since it prints an empty object and not the result that the function returns. 问题在于,要求值的函数会调用后端,并且显然在ng-if旁边无法正常工作,因为它打印的是空对象,而不是函数返回的结果。
I'm showing the result into the html with this to know what the function is responding to: 我将结果显示到html中,以了解该函数正在响应什么:
{{organizationController.isAllowedToInvite ()}}
this print to me an empty object {} 此打印给我一个空对象{}
this is the function that I am evaluating: 这是我正在评估的功能:
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);
});
};
this return a true or false, this works well by itself. 这返回的是对还是错,这本身就可以很好地工作。
Why can not I show the result of the function when I print in the html? 在html中打印时,为什么不能显示函数的结果? because instead of showing true / false it shows me an empty object {}? 因为没有显示true / false,而是显示了一个空对象{}?
i want it show me true or false, so I can evaluate it in the ng if correctly. 我希望它显示真假,因此如果可以,我可以在ng中对其进行评估。
this is my ng-if, obviusly this does not work becouse has a {} value. 这是我的ng-if,显然这不起作用,因为它具有{}值。
ng-if="organizationController.isAllowedToInvite()"
there's an async operation in your method, you cannot statically check the condition in the if. 您的方法中有一个异步操作,您不能静态检查if中的条件。 You should call the method on the controller instantiation and then use a flag that you set to true in case all the conditions are met and check that flag in the template 您应该在控制器实例化上调用该方法,然后使用设置为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();
and in your template 并在您的模板中
ng-if="organizationController.isAllowed"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.