使用来自服务器的数据更新/刷新 angularjs ng-repeat [英] update/refresh angularjs ng-repeat with data from server

查看:33
本文介绍了使用来自服务器的数据更新/刷新 angularjs ng-repeat的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图建立一个网站来喜欢一些图片.

I try to buil a website to like some pictures.

我可以加载图片,但我想有一个喜欢图片的点赞系统.

I can load the picture, but I want to have a like system with who like the picture.

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

my problem was I have to refresh the page to see who like this picture and increment the count.

这是我的控制器:

$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

推荐答案

我只是通过添加 $scope.watch 和 $scope apply 来更改我的 upVote 函数.

I just change my upVote function by adding a $scope.watch and $scope apply.

这是我的例子:

$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();
  };

这篇关于使用来自服务器的数据更新/刷新 angularjs ng-repeat的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆