如何通过角度js发送php结果数据

时间:2017-12-27 06:11:25

标签: php angularjs

这是我的html Page whare我添加了ng-app =" myApp"和ng-controller =" DashCtrl"

<form action="" ng-submit="dashForm(dashdata)" method="post"> 
        <input type="hidden" name="uid" ng-model="dashdata.uid"
       value="<?php echo $row["id"];?>">
        <input type="submit" name="edit" value="Edit">
</form>

这是角度代码

myApp.controller('DashCtrl', ['$scope', '$http', function($scope, $http) {        
            $scope.dashForm= function(dashdata) 
            {
               var encodedString = 'action=Userview'+'&id='+dashdata.uid;
               alert(encodedString);
                 $http({
                     method: 'POST',
                     url: 'ajaxprocess.php',
                     data: encodedString,
                     headers: {'Content-Type': 'application/x-www-form-urlencoded'}
                 })

                 .success(function(data) {
                        console.log(data);
                      if( data.trim() === 'admin') {
                         window.location.href ='dashbord.php';
                     } 
                     if( data.trim() === 'user') {
                         window.location.href ='test.php';
                     }  else {
                         $scope.errorMsg = data;
                         // statement
                     }
                 })            
            } 
        }]);

这给我发错了ui没有定义, 警告消息显示但是显示ui = undefined

1 个答案:

答案 0 :(得分:0)

试试这个:

<form action="" ng-submit="dashForm()" method="post"> 
        <input type="hidden" name="uid" ng-model="dashdata.uid"
           ng-init="<?php echo $row["id"];?>">
        <input type="submit" name="edit" value="Edit">
</form>

并在控制器中:

var encodedString = 'action=Userview'+'&id='+$scope.dashdata.uid;

希望它会对你有所帮助。