繁体   English   中英

AngularJs控制器不等待$ http请求

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

我是Angular的新手,对Promise有点困惑。 我创建了一个自定义指令,该指令应显示来自远程Web服务的信息。

这是一家负责数据检索的工厂:

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

这是应该显示来自此服务的信息的指令:

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

最后是指令的模板:

<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>

因此,我希望来自服务的信息显示在一个表中,但是在呈现页面时,该表为空。

您需要使用scope。$ apply才能使更改生效。 AngularJS和scope。$ apply

暂无
暂无

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

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