AngularJs $scope doesn't update after a GET request on a factory

前端 未结 5 1000
广开言路
广开言路 2020-12-31 14:10

I have been trying AngularJS for a experimental project and I came along with this problem. In my html I want to display a list of items

Index.html<

5条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-31 14:38

    I think another elegant solution to this problem could be - if you are using one of the routing libraries, in my case it is the UI-Router, but could be also ngRoute, is making your controller dependent on the response of the promise, eg. adding a resolve property to the adequate state/route which doesn't let the controller load until the promise is solved and the data is ready, so in your config:

    .state('datpage', {
          url: '/datpage',
          controller: 'DatpageController',
          resolve:{
            datData: function (datfactory) {
                return datDataService.getData("datDataParam");
            }]
          },
          templateUrl: 'views/datpage.html'
        })
    

    And inject the datData dependency in your controller, where you can apply it directly to the $scope:

    .controller('DatpageController', function ($scope,datData) {
    $scope.datPageData = datData; ...
    

提交回复
热议问题