2017-04-05 8 views
-1

PHPを使用して$scope変数を初期化して、後で$http.postリクエストでサーバーに送信できるのはどうすればよいですか?私はng-initと試みたが、動作していないよう:

<body ng-app="app" ng-controller="ctrl" ng-init="name='<?php echo $_GET['search']; ?>'"> 
    <pre> 
     {{result}} 
    </pre> 
</body> 
<script> 
    var app = angular.module('app',[]); 
    app.controller('ctrl',function($scope,$http){ 
     $http.post('post.php',{'var1': $scope.name,'var2':'test'}).then(function(response) { $scope.result = response.data; }); 
    }); 
</script> 

答えて

0

たぶん、このような何かを試してみてください。

<body ng-app="app" ng-controller="ctrl" ng-init="postMethod()"> 
    <pre> 
     {{result}} 
    </pre> 
</body> 
<script> 
    var app = angular.module('app',[]); 
    app.controller('ctrl', function ($scope, $http) { 
     $scope.myVar = '<?php echo $_GET['search']; ?>'; 
     $scope.postMethod = function(){ 
      $http.post('post.php',{'var1': $scope.myVar,'var2':'test'}).then(function(response) { $scope.result =  response.data; }); 
     }; 
    }); 
</script> 
関連する問題