将PHP变量传递给Angular

3

我将使用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;
       });
    };

2
这不是正确的处理方式。数据应该通过API从服务器获取,即使是“currentLoggedUser”。在正常的应用程序流程中,除非您登录,否则您无法查看任何内容 - 然后您已经拥有了loggedUser,这不会成为问题。我建议模拟一个实时应用程序,只需进行10行代码的登录即可。 - Dragos Rusu
@DragosRusu 这个ID不是已登录的用户,而是在列表中选择的用户。这样做行不行?(或者说这不是推荐的做法) - data2info
页面加载时选择的元素? - Dragos Rusu
@DragosRusu 页面的网址将是www.mydom.com/customer.php?id=10072。 - data2info
如果您正在使用路由,请只需使用$routeParams['id'],或者$location.search().id并将其在$scope中提供给模板。 - Dragos Rusu
1个回答

0

尝试以这种方式发布您的数据

 $scope.createBook = function(book_form,user){

    if(book_form.$invalid) {
        $scope.invalidSubmitAttempt = true;
        return;
    }else{

         $http.post(appurl+'user/adds/', user).success(function() {

});

             $scope.book_form.$setPristine();

         });

 }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接