AngularJS表单未绑定到$ http请求

时间:2021-07-20 17:59:26

 <form novalidate class="form-horizontal">
                <div class="form-group">
                    <div class="col-sm-offset-2 col-sm-10">
                        <div class="text-capitalize">

                        </div>
                    </div>
                </div>

                <div class="panel-body">
                    <div class="form-group">
                        <label for="inputUsluga3" class="col-sm-2 control-label">Usluga</label>
                        <div class="col-sm-6">
                            <select id="inputUsluga3" class="form-control">
                                <option>Kombi</option>
                                <option>Hotel</option>
                            </select>
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="inputOdDatum3" class="col-sm-2 control-label"><i class="fa fa-calendar"></i> <i class="fa fa-arrows-h"></i> <i class="fa fa-calendar"></i>Od datuma</label>
                        <div class="col-sm-6">
                            <input id="inputOdDatum3" type="text" class="form-control" ng-model="fromDate" data-max-date="{{untilDate}}" placeholder="Početak perioda" bs-datepicker data-date-format="dd.MM.yyyy" />
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="inputDoDatum3" class="col-sm-2 control-label"><i class="fa fa-calendar"></i> <i class="fa fa-arrows-h"></i> <i class="fa fa-calendar"></i>Do datuma</label>
                        <div class="col-sm-6">
                            <input id="inputDoDatum3" type="text" class="form-control" ng-model="untilDate" data-min-date="{{fromDate}}" placeholder="Kraj perioda" bs-datepicker data-date-format="dd.MM.yyyy" />
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="inputStanica" class="col-sm-2 control-label">Stanica</label>
                        <div class="col-sm-6">
                            <select id="inputStanica" ng-model="airport" class="form-control">
                                <option>PUY</option>
                                <option>ZAG</option>
                                <option>SPL</option>
                                <option>DUB</option>
                            </select>
                        </div>
                    </div>
                </div>

                <div class="panel-body">
                    <div class="form-group">
                        <div class="col-sm-offset-2 col-sm-10">
                            <button type="button" class="btn btn-default" ng-click="getData()">Dohvati podatke</button>
                        </div>
                    </div>
                </div>
            </form>

MY Http request looks like this

MY Http请求看起来像这样

        $scope.getData = function() {
            $http.get("/Services/JoppdService.svc/getKombiImport/" + $scope.fromDate + "/" + $scope.untilDate + "/" + $scope.airport)
                 .success(function (response) {
                     $scope.education = response;
                 });
        }

After submiting i have request in console that looks like this

提交后我在控制台中有这样的请求

http://localhost:8080/Services/JoppdService.svc/getKombiImport/undefined/undefined/undefined

dateFrom, dateUntil and airport is not binded. Where is problem ?

dateFrom,dateUntil和airport没有绑定。哪里有问题?

3 个解决方案

#1


1  

Use ng-submit, bind the required parameters into an object obj, like obj.fromDate, obj.untilDate, and obj.airport in the form. Use button type as submit

使用ng-submit,将所需参数绑定到对象obj中,如表单中的obj.fromDate,obj.untilDate和obj.airport。使用按钮类型作为提交

Your html would transform into,

你的HTML会变成,

<form novalidate class="form-horizontal" ng-submit="getData(obj)">
                <div class="form-group">
                    <div class="col-sm-offset-2 col-sm-10">
                        <div class="text-capitalize">

                        </div>
                    </div>
                </div>

                <div class="panel-body">
                    <div class="form-group">
                        <label for="inputUsluga3" class="col-sm-2 control-label">Usluga</label>
                        <div class="col-sm-6">
                            <select id="inputUsluga3" class="form-control">
                                <option>Kombi</option>
                                <option>Hotel</option>
                            </select>
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="inputOdDatum3" class="col-sm-2 control-label"><i class="fa fa-calendar"></i> <i class="fa fa-arrows-h"></i> <i class="fa fa-calendar"></i>Od datuma</label>
                        <div class="col-sm-6">
                            <input id="inputOdDatum3" type="text" class="form-control" ng-model="obj.fromDate" data-max-date="{{obj.formDate}}" placeholder="Početak perioda" bs-datepicker data-date-format="dd.MM.yyyy" />
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="inputDoDatum3" class="col-sm-2 control-label"><i class="fa fa-calendar"></i> <i class="fa fa-arrows-h"></i> <i class="fa fa-calendar"></i>Do datuma</label>
                        <div class="col-sm-6">
                            <input id="inputDoDatum3" type="text" class="form-control" ng-model="obj.untilDate" data-min-date="{{obj.untilDate}}" placeholder="Kraj perioda" bs-datepicker data-date-format="dd.MM.yyyy" />
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="inputStanica" class="col-sm-2 control-label">Stanica</label>
                        <div class="col-sm-6">
                            <select id="inputStanica" ng-model="obj.airport" class="form-control">
                                <option>PUY</option>
                                <option>ZAG</option>
                                <option>SPL</option>
                                <option>DUB</option>
                            </select>
                        </div>
                    </div>
                </div>

                <div class="panel-body">
                    <div class="form-group">
                        <div class="col-sm-offset-2 col-sm-10">
                            <button type="submit" class="btn btn-default">Dohvati podatke</button>
                        </div>
                    </div>
                </div>
            </form>

Pass obj in ng-submit method getData() which now looks like, getData(obj)

在ng-submit方法getData()中传递obj,现在看起来像,getData(obj)

You got to convert the fromDate and untilDate into String. Have a look at this. Replace convertToString() below with the solution mentioned in that link.

你必须将fromDate和untilDate转换为String。看看这个。使用该链接中提到的解决方案替换下面的convertToString()。

Then in your controller getData() would look like,

然后在你的控制器getData()看起来像,

$scope.getData = function(obj) {
    var fromDate = convertToString(obj.fromDate);
    var untilDate = convertToString(obj.untilDate);
    var airport = obj.airport;
            $http.get("/Services/JoppdService.svc/getKombiImport/"+fromDate+"/"+untilDate+"/"+.airport)
                 .success(function (response) {
                     $scope.education = response;
                 });
        }

Also, do not have empty spaces (" ") while forming URLs.

此外,在形成URL时不要有空格(“”)。

If you want to initialize anything, use ng-init like this

如果要初始化任何内容,请像这样使用ng-init

#2


0  

You would need to wrap your content inside if you have not already Also you should use for date inputs

如果您还没有将内容包装在内,您还需要将其用于日期输入

#3


0  

You must use ng-init to initialize these values like ng-init="fromDate = 0".

必须使用ng-init初始化这些值,如ng-init =“fromDate = 0”。

You could also just declare these variables at the begining of your controller such as $scope.fromDate = 0;

您也可以在控制器的开头声明这些变量,例如$ scope.fromDate = 0;

#1


1  

Use ng-submit, bind the required parameters into an object obj, like obj.fromDate, obj.untilDate, and obj.airport in the form. Use button type as submit

使用ng-submit,将所需参数绑定到对象obj中,如表单中的obj.fromDate,obj.untilDate和obj.airport。使用按钮类型作为提交

Your html would transform into,

你的HTML会变成,

<form novalidate class="form-horizontal" ng-submit="getData(obj)">
                <div class="form-group">
                    <div class="col-sm-offset-2 col-sm-10">
                        <div class="text-capitalize">

                        </div>
                    </div>
                </div>

                <div class="panel-body">
                    <div class="form-group">
                        <label for="inputUsluga3" class="col-sm-2 control-label">Usluga</label>
                        <div class="col-sm-6">
                            <select id="inputUsluga3" class="form-control">
                                <option>Kombi</option>
                                <option>Hotel</option>
                            </select>
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="inputOdDatum3" class="col-sm-2 control-label"><i class="fa fa-calendar"></i> <i class="fa fa-arrows-h"></i> <i class="fa fa-calendar"></i>Od datuma</label>
                        <div class="col-sm-6">
                            <input id="inputOdDatum3" type="text" class="form-control" ng-model="obj.fromDate" data-max-date="{{obj.formDate}}" placeholder="Početak perioda" bs-datepicker data-date-format="dd.MM.yyyy" />
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="inputDoDatum3" class="col-sm-2 control-label"><i class="fa fa-calendar"></i> <i class="fa fa-arrows-h"></i> <i class="fa fa-calendar"></i>Do datuma</label>
                        <div class="col-sm-6">
                            <input id="inputDoDatum3" type="text" class="form-control" ng-model="obj.untilDate" data-min-date="{{obj.untilDate}}" placeholder="Kraj perioda" bs-datepicker data-date-format="dd.MM.yyyy" />
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="inputStanica" class="col-sm-2 control-label">Stanica</label>
                        <div class="col-sm-6">
                            <select id="inputStanica" ng-model="obj.airport" class="form-control">
                                <option>PUY</option>
                                <option>ZAG</option>
                                <option>SPL</option>
                                <option>DUB</option>
                            </select>
                        </div>
                    </div>
                </div>

                <div class="panel-body">
                    <div class="form-group">
                        <div class="col-sm-offset-2 col-sm-10">
                            <button type="submit" class="btn btn-default">Dohvati podatke</button>
                        </div>
                    </div>
                </div>
            </form>

Pass obj in ng-submit method getData() which now looks like, getData(obj)

在ng-submit方法getData()中传递obj,现在看起来像,getData(obj)

You got to convert the fromDate and untilDate into String. Have a look at this. Replace convertToString() below with the solution mentioned in that link.

你必须将fromDate和untilDate转换为String。看看这个。使用该链接中提到的解决方案替换下面的convertToString()。

Then in your controller getData() would look like,

然后在你的控制器getData()看起来像,

$scope.getData = function(obj) {
    var fromDate = convertToString(obj.fromDate);
    var untilDate = convertToString(obj.untilDate);
    var airport = obj.airport;
            $http.get("/Services/JoppdService.svc/getKombiImport/"+fromDate+"/"+untilDate+"/"+.airport)
                 .success(function (response) {
                     $scope.education = response;
                 });
        }

Also, do not have empty spaces (" ") while forming URLs.

此外,在形成URL时不要有空格(“”)。

If you want to initialize anything, use ng-init like this

如果要初始化任何内容,请像这样使用ng-init

#2


0  

You would need to wrap your content inside if you have not already Also you should use for date inputs

如果您还没有将内容包装在内,您还需要将其用于日期输入

#3


0  

You must use ng-init to initialize these values like ng-init="fromDate = 0".

必须使用ng-init初始化这些值,如ng-init =“fromDate = 0”。

You could also just declare these variables at the begining of your controller such as $scope.fromDate = 0;

您也可以在控制器的开头声明这些变量,例如$ scope.fromDate = 0;