【问题标题】:update/refresh angularjs ng-repeat with data from server使用来自服务器的数据更新/刷新 angularjs ng-repeat
【发布时间】:2015-05-15 11:36:17
【问题描述】:

我尝试建立一个网站来点赞一些图片。

我可以加载图片,但我想要一个喜欢图片的人的系统。

我的问题是我必须刷新页面才能看到谁喜欢这张照片并增加计数。

这是我的控制器:

$scope.loadingpics = function (){
        $http.get('/api/photos')
        .success(function(awesomeThings) {

          console.log(awesomeThings);
          $scope.firstname = awesomeThings;
          })
          .error(function (err){
              $scope.errors.other = err.message;
            });
         };
$scope.upVote = function(index){
         $scope.vote = 1;
         var ref = index;
         var nom = index.url.substr(30, 40);
         var num = index.vote + 1;

         $http.put('api/photos/' + nom, {
          email: email, 
          vote: num
          })
         .success(function (data) {
              $scope.firstname = data;
              $scope.loadingpics();
          })          
          .error(function (err){
            $scope.errors.other = err.message;
          });
      };

这是我的看法:

  <li ng-repeat="image in firstname | orderBy: 'firstname'"  ng-show="isLoggedIn()" class="thumbnail" title="Image 1" on-last-repeat>                                   
<img  ng-src="../assets/images/Pouce.png" ng-click="upVote(image)" data-toggle="popover" data-content="And here's some amazing content. It's very engaging. Right?" data-placement="right" title="Popover title">
            </li>

这是我的架构:

var PhotoSchema = new Schema({
  url: String,
  firstname: String,
  email: [String],      
  info: String,
  vote: Number,
  active: Boolean
});

感谢您的帮助:D

【问题讨论】:

    标签: angularjs scope refresh ng-repeat


    【解决方案1】:

    不要只在你的模式中存储喜欢的数量,而是存储一个对象数组:

     var PhotoSchema = new Schema({
      url: String,
      firstname: String,
      email: [String],
      info: String,
      vote: [{
        date: Date,
        user: String
      }],
      active: Boolean
    });
    

    【讨论】:

    • 感谢您的回答,但是对我不起作用,我必须按F5刷新页面才能查看喜欢图片的人和喜欢的数量。
    【解决方案2】:

    我只是通过添加 $scope.watch 和 $scope apply 来更改我的 upVote 功能。

    这是我的例子:

    $scope.upVote = function(index){
             $scope.vote = 1;
             var ref = index;
             var nom = index.url.substr(30, 40);
             console.log(nom);
             console.log(index.vote);
             var num = index.vote + 1;
             console.log(index);
             $http.put('api/photos/' + nom, {
              email: email, 
              })
             .success(function (data) {
              $scope.firstname = data;            
              })          
              .error(function (err){
                $scope.errors.other = err.message;
              });
               $scope.$watch($scope.firstname, $scope.loadingpics());
               $scope.apply();
      };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-02-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-09
      • 2012-08-29
      • 2016-02-12
      • 2014-04-24
      相关资源
      最近更新 更多