【问题标题】:How do I get my request data into my $scope如何将我的请求数据放入我的 $scope
【发布时间】:2016-11-21 17:13:38
【问题描述】:

我目前真的坚持这个话题。

我想将从 Express 检索到的响应数据附加到我的角度 $scope,然后将用户重定向到他们的个人资料页面。

我的控制器函数如下所示:

$scope.login = function($event){
    $event.stopPropagation();
    $http({
      method: 'POST',
      url: '/login',
      data: {
          email: $scope.email,
          password: $scope.password
      }
  }).success(function(data){
      console.log(data.events)
      $scope.events = data.events
      $location.path('/profile');
  }).error(function(err){
      console.log(err)
  })

一旦我尝试登录,我就会得到记录的数据,然后我会根据需要重定向。但是我的 ng-repeat 指令没有更新。

我记录的数据的 JSON 输出:

[{
"type":"fussball",
"distance":"1",
"date":"Morgen",
"time":"10:30",
"maxAttendees":10,
"attendees:[
1345455466,
323232324,
454554534343,
898493839489,
892839283928,
283293283983]}

ng-重复部分:

<div ng-class="{'first': $first}" class="event-item"ng-repeat="event in events">
    <div class="event-type {{event.type}}"></div>
    <div class="event-seperator"></div>
    <div class="event-body">
        <div class="event-body-inner">
            <span class="date"></span>
            <p class="main">{{event.date}}</p>
            <p class="sub">{{event.time}}</p>
        </div>
        <div class="event-body-inner">
            <span class="attendees"></span>
            <p class="main">{{event.attendees.length}}{{checkAttendees($index)}}</p>
            <p class="sub">TEILNEHMER</p>
        </div>
        <div class="event-body-inner">
            <span class="distance"></span>
            <p class="main">{{event.distance}}</p>
            <p class="sub">KM</p>
        </div>
    </div>
    <div class="event-seperator"></div>
    <div ng-click="selecEvent($event)" class="eventGo hvr-bounce-to-left">
        <p>GO</p>
    </div>
    <div class="event-clicked">
        <p>+  ZU <a href="/#/events">DEINEN EVENTS</a> HINZUGEFÜGT</p>
    </div>

我实际上对异步/同步编程不太熟悉,所以我希望有人能给我一个提示;)。

提前致谢!

【问题讨论】:

  • 发布你的 json 和 ng-repeat 部分
  • 我已经将 data.events 更改为仅数据,但 $location 更改仍然没有任何变化!我试图将该 ng-repeat 粘贴到我的登录模板并删除重定向,然后所有项目都显示出来了。

标签: javascript angularjs express asynchronous passport.js


【解决方案1】:

你需要告诉 Angular 事件列表上有一个异步更新

  }).success(function(data){
  console.log(data.events)
  $scope.events = data.events
  $scope.$apply(); // <----- try to add this line
  $location.path('/profile');

也许你想阅读:

When to use $scope.$apply()

观看:

Difference between $apply and $digest

【讨论】:

  • 我已经试过这个 nur 我得到一个摘要循环正在运行的错误!
【解决方案2】:

最好的方法是为此使用工厂。我正要写它,但我在另一篇文章中发现了另一个问题。

如果您喜欢评论,请为原始海报投票。

https://stackoverflow.com/a/21855302/1549291

编辑:为实现添加了 plunker 链接: https://plnkr.co/edit/EimStRS1yMPbiU6PWKJN

(function () {
//factory

angular.module('app')
.factory('appFactory', ['$http', function ($http){

  this.getlist = function(){            
        return $http.get('data.json')
            .then(function(response) {
              return response.data;
            });            
    };
    return this;
}]);

}());

angular.module('app').controller('appCtrl', ['$scope', '$http', '$log','$location', 'appFactory',
//controller   
    function($scope, $http, $log, $location, appFactory) {

    // Chapters  json data
    appFactory.getlist()
    .then(function(data) {
        $scope.events = data;
    });

}]);

【讨论】:

  • 你认为我可以使用工厂函数而不发送另一个 get 请求而只传递现有响应吗?
  • 根据该示例,只有一个 GET 实例正在执行。您可以在需要进行调用的任何时候使用此工厂。
  • 我在工厂和服务中尝试过,但两者都不起作用。但是感谢您的尝试;)
  • 我使用 JSON 作为输入在 plunker 中实现了您的代码。看看这个。它被剥离但有效。
猜你喜欢
  • 1970-01-01
  • 2021-01-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-17
  • 1970-01-01
  • 2021-09-05
相关资源
最近更新 更多