mysql - 自动刷新$scope

标签 mysql angularjs refresh



我正在制作一个 Angular Recipe 应用程序,它从 MySQL 数据库检索数据并将其显示在屏幕上。我有一个“喜欢”食谱的功能,它只是加上了总喜欢数。但每次我喜欢它,我都必须刷新页面。我希望范围能够自行刷新。我尝试使用push()函数和apply(),但我无法让它工作。有人可以帮我吗?

Dishes.html

<div class="" ng-controller='dishesController' >

<div class="dishPanel" ng-repeat='dish in dishes'>
    <div class="imageDishPanel">
        <img ng-src={{dish.link}} />
        <div class="slideBoxDown custFont titleDishPanel">
            {{dish.name}}
        </div>

        <div class="slideBoxUp">
            <div class=" infoTextDishPanel custFont">
                {{dish.info}}
            </div>
        </div>
    </div>

    <div class="loveDishPanel">
        <div ng-click='getID(dish.id, dish.likes)' class="heart"></div>
        <div class="likes custFont">{{dish.likes}} likes</div>
    </div> 
</div>

dishesController.js

app.controller('dishesController', ["$scope", "$http","dishesFactory", function($scope, $http, dishesFactory){
dishesFactory.getDataDishesFactory(function(data){
    $scope.dishes = data;
});

$scope.getID = function(id,like){
    $scope.likes  ={
        "id": id,
        "like": like
    };
    $scope.updateLike();
};

$scope.updateLike = function(){
    $http.post("php/updateLike.php",$scope.likes)
        .success(function(data){

        }
    );
};
}]);

编辑:所以我正在做的是显示数据库中的所有菜肴。每道菜都有他的“赞”数和唯一 ID。如果您按“like”,它将调用 getID(id, like);函数,并且调用 updateLike 函数。 updateLike 函数会将数据(id 和类似计数)发送到 updateLike.php 文件,并从那里将原始计数加 1。新的计数将在数据库中更新。所以在 html 页面上我必须自己刷新它才能看到新的计数。我想让计数自行刷新。为此,我尝试使用 $scope.dishes[$scope.id].likes.push(data) 从 Controller 中的成功回调中推送数据。所以我必须将新的点赞计数推到正确的菜肴上,因为它是一系列菜肴。但我无法将它推到正确的盘子上..

updateLike.php

<?php
require 'connect.php';
$data = json_decode(file_get_contents("php://input"));

$id =  $data->id;
$like = (int) $data->like;

$like++;
//$like = var_dump($data->like);

$query = "UPDATE `recipes` SET `likes`='$like' WHERE `id` = '$id' ";
mysql_query($query,$con);


echo $like;

最佳答案

你把事情搞得太复杂了。你想做的很简单。

进行以下更改

在 html 中

         <div ng-click='getID(dish)' class="heart"></div>

在 Controller 中

$scope.getID = function(dish){
    //Ideally dish.like++ would be done here itself to give instant feeling
    like  ={
        "id": dish.id,
        "like": dish.like
    };
    $scope.updateLike(like, dish);
};

$scope.updateLike = function(like, dish){
    $http.post("php/updateLike.php",like)
        .success(function(data){
            dish.like=data.like;//under the impression the response have like count;
         }
        ).error(function(data){
           //and do dish.like-- if an error happens.
        });
 };

关于mysql - 自动刷新$scope,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23627107/

相关文章:

java - 如何使 Siteminder session 失效

android - Android ListView 如何刷新?

php - MySQL - 复杂查询问题,多个表

mysql - 在选择标签中显示当前用户的数据及其名称

javascript - AngularJS : cannot connect factory to controller

c# - 如何刷新 DbContext

javascript - 在集合中的二级更改后重新渲染 ng-options

mysql - MySQL 中何时使用单引号、双引号和反引号

mysql - 使用 Sakila 的子查询问题

javascript - cordova-plugin-screen-orientation 不适用于特定页面