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