繁体   English   中英

angular js ng-if没有从异步函数获取返回值

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM