使用 $http.post , angularJs 发送 ng-init 变量

send ng-init variable with $http.post , angularJs

如何使用 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>

也许可以试试这样:

<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>