Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.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
Angularjs 将嵌套的ng repeat中的ng model设置为params以进行ajax调用_Angularjs - Fatal编程技术网

Angularjs 将嵌套的ng repeat中的ng model设置为params以进行ajax调用

Angularjs 将嵌套的ng repeat中的ng model设置为params以进行ajax调用,angularjs,Angularjs,现在我想用ng模型值作为参数进行ajax调用?我该怎么做? 我将ng模型设置为json数据,这是一个值。一切正常。我可以这样做吗?如何在controller中设置ng模型的范围 控制器 EZlearn.controller("testController", function($scope, $http) { $scope.test="false"; alert($scope.questions); $scope.startTest = function(){ alert("star

现在我想用ng模型值作为参数进行ajax调用?我该怎么做? 我将ng模型设置为json数据,这是一个值。一切正常。我可以这样做吗?如何在controller中设置ng模型的范围

控制器

EZlearn.controller("testController", function($scope, $http) {
$scope.test="false";
alert($scope.questions);
$scope.startTest = function(){  
    alert("starttest");
    $http({
        method : 'POST',
        url : 'startTest'
    }).success(function(data,status,headers,config){
        $scope.questions  = data;
        alert($scope.questions);
        if($scope.questions!=""){
            $scope.test="true"
            //window.location.href="welcome.jsp";
        }
    }).error(function(data,status,headers,config){
        alert("");
    });
}  })
Html

<div data-ng-show="test" class="row">
    <form class="col-md-offset-2 col-md-8" action="evaluateTest" method="post">
        <div data-ng-repeat="qus in questions" data-ng-init="value=$index+1">
            <div class="form-group">
                <label>{{value}}. {{qus.question}}</label>  
                <div data-ng-if="qus.questionType=='Radio'">
                    <div data-ng-repeat="options in qus.options">
                        <input type="radio" name="answer{{value}}" data-ng-model="qus.questionNumber" value="{{options}}" >{{options}}
                    </div>
                </div>
                <div data-ng-if="qus.questionType=='Text'">
                    <input type="text" name="answer{{value}}" data-ng-model="qus.questionNumber" >
                </div>  
            </div>
        </div>  
        <input type="Submit" value="Submit Test" class="btn btn-success">
    </form>
</div>  

多一点上下文会有更好的答案,但我会尽我所能概括这一点

据所知,您不能使用ng模型调用函数(ajax将位于其中)。ng模型的要点是设置控制器范围内的变量值。如果你想调用一个函数(我猜是值、状态或状态的变化),你可以用$watch来实现

让watch函数检查ng模型中设置的变量的值,当它被更改时,您可以检查新值,并在触发watch时执行“填充”。以下是指向手表示例的链接,您可以将其用作参考:

我建议创建一个范围变量并将其设置为任意值,然后将ng模型指向您的变量。当手表按下时,您可以检查新值,如果它是您想要/期望的,请执行ajax请求

此外,根据您使用的UI框架/包的不同,您可以调用异步函数。例如,在Angular Material中,select(一个下拉元素)可以在open上设置一个属性call md,该属性将调用一个函数(在该函数中执行ajax请求)

如果这没有帮助,请提供更多关于您正在做什么以及您正在使用哪些元素的见解

祝你好运

尝试以下操作(用于作为传统GET请求参数发送):

如果要指定请求后正文,请使用数据代替参数

    $http({
    method: 'POST',
    url: '--your url ---',
    data: $scope.questions

    ).then(function(res){
        //success callback
   }, function(res){
     //failure callback
    });

当然,如果不打算发送$scope.questions,请将其替换为所需的数组。注意你发送的是一个对象。

首先你需要注意的是,如果你有angular 1.5.9,在这个版本中,
$hhtp.success
函数不推荐使用,所以最好使用
$http.then
函数

现在转到您的问题,我认为有一个错误,我们的
数据
对象
数据。问题
数组
,您需要传递
ng repeat
见下文

EZlearn.controller(“testController”,函数($scope,$http){
$scope.test=“false”;
警报($scope.questions);
$scope.startTest=function(){
警报(“启动测试”);
$http({
方法:“POST”,
url:“startTest”
}).success(函数(数据、状态、标题、配置){

//$scope.questions=data;//请提供更多信息。jsfiddle会很好。您想发送什么数据?questions数组是ajax调用的输入或输出?我想发送相应的radiobuttonvalue(这是问题的答案)作为http请求的输入参数。抱歉,我认为这是一个误解。实际上,通过调用startTest(),我从后端获取数据(Json)并使用ng repeat显示它,现在我想检索用户选择的数据(单选按钮)。
    $http({
    method: 'GET',
    url: '--your url ---',
    params: $scope.questions

    ).then(function(res){
        //success callback
   }, function(res){
     //failure callback
    });
    $http({
    method: 'POST',
    url: '--your url ---',
    data: $scope.questions

    ).then(function(res){
        //success callback
   }, function(res){
     //failure callback
    });
EZlearn.controller("testController", function($scope, $http) {
$scope.test="false";
alert($scope.questions);
$scope.startTest = function(){  
    alert("starttest");
    $http({
        method : 'POST',
        url : 'startTest'
    }).success(function(data,status,headers,config){
//        $scope.questions  = data; //     <======== Here is problem 
        $scope.questions  = data.questions; //DO this instead 
        alert($scope.questions);
        if($scope.questions!=""){
            $scope.test="true"
            //window.location.href="welcome.jsp";
        }
    }).error(function(data,status,headers,config){
        alert("");
    });
}  })
<div data-ng-show="test" class="row">
    <form class="col-md-offset-2 col-md-8" action="evaluateTest" method="post"> 

        <div data-ng-repeat="qus in questions" data-ng-init="value=$index+1">
            <div class="form-group">
                        <label>{{value}}. {{qus.question}}</label>  
            <div data-ng-if="qus.questionType=='Radio'">
                <div data-ng-repeat="options in qus.options">
                    <input type="radio" name="answer{{value}}" data-ng-model="qus.questionNumber" value="{{options}}" >{{options}}
                </div>
            </div>
            <div data-ng-if="qus.questionType=='Text'">
                    <input type="text" name="answer{{value}}" data-ng-model="qus.questionNumber" >
            </div>  
            </div>
        </div>  
        <input type="Submit" value="Submit Test" class="btn btn-success">
    </form>
</div>