2012-09-03 108 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