【问题标题】:http GET and AngularJS UI Routerhttp GET 和 AngularJS UI 路由器
【发布时间】:2014-09-03 08:04:10
【问题描述】:

我正在使用 UI 路由器构建我的第一个 Angular 应用程序,我需要有关如何在模板加载时执行 http Get 的帮助。谢谢!

我已经设置了一个 plunker: http://embed.plnkr.co/LRSGXqxjtbYcr6rjTj69/preview

我需要传递一个用户 ID 以从数据库中获取正确的信息。

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' }
    ];
});

【问题讨论】:

    标签: angularjs angular-ui-router


    【解决方案1】:

    你可以监听事件$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;
            });
    
        });
    });
    

    更新 好的,我明白了这个问题,你需要监听 $viewContentLoaded,检查这个 plunker http://plnkr.co/edit/2hdjcUqd3cGAvn62jDxW?p=preview

    【讨论】:

    • 谢谢。我对此很陌生,所以我需要一个更具体的例子来确定我害怕做什么。
    • 谢谢。我很感激。有些东西不工作。项目未列出。我正在使用 listSpecs.html 作为数据。请您快速看一下我的 Plunker:embed.plnkr.co/LRSGXqxjtbYcr6rjTj69
    • 我添加了 plunker 列表,检查一下 :)
    • 谢谢!真的很感激。如何通过 http get 传递 userId,以便从数据库中获取正确的信息。 UserId 需要在应用程序中“全局”可用,并且会在应用程序启动时加载。
    • 此外,如果 GET 返回零结果(如果用户尚未创建任何项目,则不是错误)。如何隐藏表格并显示文本? “你还没有添加任何规范。”
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-03-16
    • 2023-03-30
    • 1970-01-01
    • 2016-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多