2012-09-03 106 views
0

我希望通过AngularJS和PHP在MySQL数据库中添加记录。该记录已成功添加,但未看到AngularJS效果。即该页面不会自动刷新。下面给出的代码:angularjs插入页面刷新

控制器

$scope.add = function() { 
       var elem = angular.element($element); 
       var dt = $(elem).serialize(); 
       //alert($element); 
       console.log($(elem).serialize()); 
       $http({ 
        method: 'POST', 
        url: 'php/add.php', 
        data: dt, 
        headers: {'Content-Type': 'application/x-www-form-urlencoded'} 
       }).success(function(data, status) { 
        $scope.status = status; 
        $scope.data = data; 
        console.log(data); 
        data.push(this); 
        $scope.products = data; // Show result from server in our <pre></pre> element 
       }).error(function(data, status) { 
        $scope.data = data || "Request failed"; 
        $scope.status = status; 
       }); 
      }; 

的index.html

<li ng-repeat="product in products" ng-model = 'products'> 
       {{product.description}}|{{product.name}} | <a href='edit.html' ng-click = "edit(product.product_id)">edit</a> | <a href='' ng-click = "del(product.product_id)" ng-hide="isHidden">delete</a> 
       <input type="hidden" name="hdnid" ng-model="hdn" value="{{product.product_id}}"/> 
       </li> 

add.php

<?php 
include 'connect.php'; 
mysql_select_db($database,$con); 
$nm = $_POST['keywords']; 
$desc = $_POST['desc']; 
$query = "INSERT INTO `product`(`name`,`description`) VALUES ('$nm', '$desc')"; 
$result = mysql_query($query) OR die(mysql_error()); 
?> 

如何自动刷新页面 ?

回答

0

当您分配新阵列或修改现有的$ scope.products时,它会自动列出html中新/修改列表中的每个产品。您不必重新加载页面。

在你的情况下,该帖子的响应数据应该是json数组,如果不是,它不会工作。

类似mysql_query($ query).toJSON应该可以工作。

如果你想刷新页面(为了一些非原因),你应该在http调用成功时明确地做到这一点。请记住,使用$ http包装进行的GET/POST调用是ajax调用。

例如:http://jsfiddle.net/Y3b3H/12/

+0

http://jsfiddle.net/Y3b3H/12/可以看看下面这个例子.. – manoj