2015-10-06 176 views
3

我正在使用angular将注释添加到列表中。这工作正常。但我坚持添加一个PHP变量,将其保存到我的列表中。将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不会传递到角度。只有当我点击添加按钮。因此,新评论会添加到正确的ID中,但旧评论仅在添加新评论后才可见。 如何在页面加载时获得角度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”。在正常的应用程序流程中,除非您登录,否则不允许查看任何内容 - 然后您已经拥有已登录的用户,这不会成为问题。 我会建议模拟一个实时应用程序,只是做一个10-liner登录。 –

+0

@DragosRusu ID不是已登录的用户,它是列表中的选定用户。这样做是不可能的吗? (或者不是推荐的方式) – data2info

+0

页面加载时选定的元素? –

回答

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(); 

     }); 

} 
}