2014-09-03 93 views
0

我正在用UI路由器構建我的第一個Angular應用程序,我需要關於如何在模板加載時執行http獲取的幫助。謝謝!http GET和AngularJS UI路由器

我已經成立了一個plunker: http://embed.plnkr.co/LRSGXqxjtbYcr6rjTj69/preview

我需要通過一個用戶ID來從DB獲得正確的信息。

app.controller('SpecsController', function ($scope) { 
    //I want to get this data with an AJAX call every time specs.html is loaded: 
    $scope.specsList = [ 
    { title: 'ISBN', lastModified: '2014-12-12', usedIn: 2, id: '123' }, 
    { title: 'Brand', lastModified: '2013-11-10', usedIn: 5, id: '456' } 
    ]; 
}); 

回答

0

你可以聽事件$ stateChangeStart

入住這裏 https://github.com/angular-ui/ui-router/wiki#state-change-events

文檔編輯

像這樣的事情

app.controller('SpecsController', function ($rootScope, $scope, $http) { 
    $rootScope.$on('$stateChangeStart', 
    function(event, toState, toParams, fromState, fromParams){ 
     $http.get('url') 
     .then(function(result) { 
      $scope.specsList = result.data; 
     }); 

    }); 
}); 

UPDATE 沒關係,我理解這個問題,你需要監聽$ viewContentLoaded,檢查此plunker http://plnkr.co/edit/2hdjcUqd3cGAvn62jDxW?p=preview

+0

感謝。我全都是新手,所以我需要一個更具體的例子來確定我害怕做什麼。 – peta 2014-09-03 08:32:00

+0

看到答案,編輯 – gaskar 2014-09-03 09:14:41

+0

謝謝。我很感激。有些東西不起作用。這些項目未列出。我使用listSpecs.html作爲數據。請你能快速看看我的Plunker:http://embed.plnkr.co/LRSGXqxjtbYcr6rjTj69/ – peta 2014-09-04 09:03:14