简体   繁体   English

AngularJs控制器不等待$ http请求

[英]AngularJs controller doesn't wait for $http request

I'm new to Angular and a little confused with promises. 我是Angular的新手,对Promise有点困惑。 I've created a custom directive that should show the information coming from a remote web service. 我创建了一个自定义指令,该指令应显示来自远程Web服务的信息。

Here's a factory responsible for data retrieval : 这是一家负责数据检索的工厂:

myAppModule.factory('Data', function ($http)
{
    var PID = 7499;
    return $http({method: "GET", url: "http://localhost:7001/Nama/services/helloworld/bookingInit", params: {pid: PID}});
});

This is the directive that should show the information coming from this service : 这是应该显示来自此服务的信息的指令:

    myAppModule.directive("timePeriods", function ()
{
    return {
        restrict: 'E',
        templateUrl: 'timePeriods.html',
        controllerAs: 'periodsCtrl',
        controller: ['Data', function (Data)
        {
            Data.success(function (res)
                                     {
                                         this.periods = res.appointments;
                                     });
        }]
    }
});

finally here's the template for the directive : 最后是指令的模板:

<table class="table table-bordered" ng-class="'noselect'">
  <td colspan="2">
    <h4>TIMESLOTS</h4>
  </td>
  <tr ng-repeat="period in periodsCtrl.periods">
    <td>
      <div>{{period.start | date : 'hh:mm'}} - {{period.end | date : 'hh:mm'}}</div>
    </td>
  </tr>
</table>

So I expect the information from the service to be displayed in a table, but when the page is rendered this table is empty.. 因此,我希望来自服务的信息显示在一个表中,但是在呈现页面时,该表为空。

You need to use scope.$apply for the changes to take effect. 您需要使用scope。$ apply才能使更改生效。 AngularJS and scope.$apply AngularJS和scope。$ apply

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

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