如何将异步功能的结果传递给Angular模板

时间:2018-06-02 20:00:28

标签: javascript angular

我的async函数权限()工作正常,但我不知道如何将此函数的结果传递给角度模板。

我有这个代码.ts文件:

public void ConfigureServices(IServiceCollection services)
     {
         services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
         services.AddIdentity<ApplicationUser, IdentityRole>(config => {
                    config.SignIn.RequireConfirmedEmail = true;
         }).AddEntityFrameworkStores<ApplicationDbContext>()
           .AddDefaultTokenProviders();
            // Add application services.
            services.AddTransient<IEmailSender, EmailSender>();
            services.AddTransient<IUserRepository, UserRepository>();
            services.AddTransient<IInvestmentRepository, InvestmentRepository>();
            services.AddTransient<IProjectRepository, ProjectRepository>();
            services.AddTransient<IPortfolioRepository, PortfolioRepository>();
            services.AddTransient<IHttpContextAccessor, HttpContextAccessor>();
            services.AddTransient<IPaypalRepository, PaypalRepository>();
            services.AddTransient<IStripeRepository, StripeRepository>();
            services.AddTransient<IOrderRepository, OrderRepository>();
            services.AddTransient<IComissionRepository, ComissionRepository>();
            services.AddTransient<IWithdrawRepository, WithdrawRepository>();
            services.AddTransient<IDepositRepository, DepositRepository>();
            services.AddAutoMapper();
            services.AddDistributedMemoryCache();
            services.AddSession();
            services.AddCors();
            services.AddMvc();
        }

这是我模板的一部分:

async havePermission(channelId) {
        console.log('is private' + ' ' + await this.isPrivateChannel2(channelId));
        console.log('in channel' + ' ' + await this.inChannel(channelId));
        if (await this.isPrivateChannel2(channelId) && !(await this.inChannel(channelId))) {
            console.log('no permission');
            return false;
        } else {
            console.log('have permission');
            return true;
        }
    }

    async permission(channelId) {
        return await this.havePermission(channelId);
    }
}

app-chat组件总是显示,即使权限返回false,为什么?

1 个答案:

答案 0 :(得分:0)

为了将promise(或任何异步操作,Observables)的结果绑定到模板,您需要使用async管道,如:

<app-chat *ngIf="permission(channelId) | async" [channelId]="channelId"></app-chat>

有关详细信息,请查看文档:

https://angular.io/api/common/AsyncPipe