AngularJS表单未绑定到$ http请求

时间:2015-07-15 14:54:20

标签: javascript angularjs http get

 <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请求看起来像这样

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

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

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

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

3 个答案:

答案 0 :(得分:1)

使用ng-submit,将所需参数绑定到对象obj,例如表单中的obj.fromDateobj.untilDateobj.airport。使用按钮类型为submit

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

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

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

然后在您的控制器中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;
                 });
        }

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

如果您要初始化任何内容,请使用ng-init之类的String[] values = sContent.split(","); Map<Integer, String> result = IntStream.range(0, values.length) .boxed() .collect(toMap(Function.identity(), i -> values[i]));

答案 1 :(得分:0)

如果您还没有内容,则需要将内容包装在内 您还应该使用日期输入

答案 2 :(得分:0)

您必须使用ng-init初始化这些值,例如ng-init="fromDate = 0"

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