Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用$http.post和angularJs发送ng init变量_Php_Angularjs_Angularjs Ng Init - Fatal编程技术网

Php 使用$http.post和angularJs发送ng init变量

Php 使用$http.post和angularJs发送ng init变量,php,angularjs,angularjs-ng-init,Php,Angularjs,Angularjs Ng Init,如何使用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>

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

也许可以尝试以下方法:


{{result}}
var-app=angular.module('app',[]);
app.controller('ctrl',函数($scope,$http){
$scope.myVar='';
$scope.postMethod=函数(){
$http.post('post.php',{'var1':$scope.myVar,'var2':'test'});
};
});
<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>