【问题标题】:Pass php variable to angular将php变量传递给角度
【发布时间】:2016-01-03 08:46:36
【问题描述】:

我正在使用 Angular 将 cmets 添加到列表中。这工作正常。但我坚持添加一个 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 中,但旧的 cmets 只有在添加新评论后才可见。 页面加载时如何获取角度 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;
       });
    };

【问题讨论】:

  • 这不是继续的方法。数据应该来自 API 中的服务器,甚至是“currentLoggedUser”。在正常的应用程序流程中,除非您登录,否则您不能查看任何内容 - 然后您已经拥有了 loggedUser,这不会成为问题。我建议模拟一个实时应用程序,然后进行 10 行登录。
  • @DragosRusu ID 不是loggedUser,它是列表中选定的用户。不能让它以这种方式工作吗? (或者只是不推荐这样做)
  • 页面加载时选中的元素?
  • @DragosRusu 页面 url 将是 www.mydom.com/customer.php?id=10072
  • 因此,如果您正在使用路由,只需使用 $routeParams['id'] 或 $location.search().id 并使其在模板的 $scope 上可用

标签: javascript php mysql angularjs


【解决方案1】:

尝试像这样发布您的数据

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

         });

 }
}

【讨论】:

    猜你喜欢
    • 2015-09-22
    • 2017-09-17
    • 2020-01-24
    • 2016-02-21
    • 2023-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多