我将使用Angular为列表添加评论。这一步已经完成得很好。但是,我遇到了一个问题:如何将PHP变量添加到我的列表中以保存评论。
HTML:
<div class="col-lg-6 col-md-12" ng-controller="customerCommentController" ng-init="userInit('<?php echo $_GET['id'] ?>')" >
<div class="box">
<div class="box-header">
<h3 class="box-title"><?php echo $lang['LBL_CUSTOMER_COMMENTS']; ?></h3>
</div><!-- /.box-header -->
<div class="box-body">
<div ng-include src="'php/layout_customer_comments.php'"</div>
</div><!-- /.box-body -->
</div><!-- /.box -->
</div><!-- /.col -->
JS
var app = angular.module('customerCommentApp', []);
app.controller('customerCommentController', function($scope, $http) {
$scope.userInit = function(uid) {
$scope.user = uid;
};
getItem();
function getItem() {
$http.post("php/getCustomerComment.php").success(function(data){
$scope.items = data;
});
};
$scope.addItem = function (item) {
$http.post("php/addCustomerComment.php?item="+item+"&customerId="+$scope.user).success(function(data){
getItem();
$scope.itemInput = "";
});
};
});
我希望“ID”也能保存在我的mysql中。但是我无法让它工作。
编辑: 我已经让它工作了。代码没问题,但是我的addCustomerComment.php有问题。 但是另一个问题出现了。打开页面时,ID没有传递到angular中。只有当我点击添加按钮时才会传递。因此,新评论被添加到正确的ID中,但旧评论只有在添加新评论后才可见。 如何在页面加载时获取angular中的ID?
JS:
$scope.userInit = function(uid) {
$scope.user = uid;
//$scope.user = '99999';
};
// Load all available items
getItem();
function getItem(){
$http.post("php/getCustomerComment.php?customerId="+$scope.user).success(function(data){
$scope.items = data;
});
};