简体   繁体   English

在AngularJs中API响应之前呈现模板

[英]Rendering of template before a response from API in AngularJs

I am using angular-slick library to display items on a slide via ng-repeat . 我正在使用angular-slick库通过ng-repeat在幻灯片上显示项目。 The items do I get with an asynchronous call to a API. 我通过异步调用API获得项目。 The problem I have is that the template is rendered before a response from the API. 我的问题是模板是在API响应之前呈现的。

This is my controller 这是我的控制器

    angular.module('myApp')
  .controller('homepageCtrl', ['$scope', '$routeParams', '$http', function($scope, $routeParams, $http) {
    $http({method: 'GET', url: 'https://myapi.com/xxxxxx'}).
    success(function(data, status, headers, config) {
            $scope.articlesSlide = data.articles
    });
  }]); 

This is my template 这是我的模板

<slick infinite="true" dots="true" arrows="true">
          <div ng-repeat="articleSlide in articlesSlide">

          <a href="/{{articleSlide.cat}}/articulo/{{articleSlide.slug}}" class="dentro" title="Ver noticia">
            <div class="imagen"><img ng-src="{{articleSlide.image}}" alt="{{articleSlide.title}}" /></div>
            <div class="texto">
              <div class="inner">
                <div class="cat">{{articleSlide.0.cat}}</div>
                <h2>{{articleSlide.title}}</h2>
              </div>
            </div>
          </a>

          </div>
          </slick>

You need to make use of promise in order to wait for the http call to fetch the response from the server. 您需要使用promise,以便等待http调用从服务器获取响应。 After the promise is resolved then only it should populate the articlesSlide scope variable. 兑现承诺后,只有其应填充articleSlide范围变量。 In the template have a ng-show check on the articlesSlide variable so that when it is null then nothing is displayed (till the time promise is resolved) 在模板中,对articlesSlide变量进行ng-show检查,以便在该变量为null时不显示任何内容(直到时间承诺得到解决)

DOCUMENTATION: 资料:

Angular Promise 角度承诺

Example

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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