ng-click ng-keypress传递表单数据不起作用

时间:2014-08-12 11:50:53

标签: angularjs keypress angularjs-ng-click

我试图从用户输入发送数据,以便在按钮点击时在屏幕上显示。 问题是如果我点击按钮它只是传递到下一个值而不收集信息并显示在屏幕上。如果我按ENTER键它应该如何工作。我在互联网上搜索了几个例子,但根本无法让它工作。我现在正在使用:

    <button class="btn btn-lg btn-default next bt_submits" type="button" ng-click="addData(newData)">
            &nbsp;<span class="bt_arrow"></span>
    </button>

完整的HTML:

<div class="boxContent col-md-12">
        <h1>{{lang.sec101.h1}}</h1>
        <div class="col-md-12 lineBorder noPad" >
            <div class="box1">
                <p>
                    {{lang.sec101.title}}
                </p>
            </div>
            <!-- dynamic form -->
            <div class="col-md-12 borderBox boxLeft bg_box">
                <form novalidate name="addForm">
                    <div class="boxLeft" ng-show="Question != ''">
                        <div ng-show="Question.infotip != 'yes'">
                            <h1 class="heading_left">{{Question.ask}}</h1>
                        </div>
                        <div ng-show="Question.infotip == 'yes'">
                            <h1 class="heading_left info">{{Question.ask}}
                                <span class="infotip yourData" tooltip-placement="top" tooltip-trigger="click" tooltip="{{Question.infotipText}}"></span>
                            </h1>
                        </div>
                    </div>
                    <div class="boxRight" ng-show="Question != ''">
                        <button class="btn btn-lg btn-default next bt_submits" type="button" ng-click="addData(newData)">
                            &nbsp;<span class="bt_arrow"></span>
                        </button>
                    </div>
                    <div class="boxRejestracjaInput">
                        <!-- <div id="legg-select" ng-if="Question.type === 'select'">
                            <select ng-options="opt.val for opt in Question.options" name="{{Question.name}}" ng-model="value" ng-keypress="enter($event,value.val)"></select>
                        </div> -->
                        <div class="newSelect">
                            <label ng-if="Question.type === 'select'">
                                <select ng-options="opt.val for opt in Question.options" name="{{Question.name}}" ng-model="value" ng-keypress="enter($event,value.val)"></select>
                            </label>
                        </div>



                        <input
                            ng-if="Question.type === 'text'"
                            type="{{Question.type}}"
                            name="institutionName"
                            class="inputName"
                            value="{{Question.value}}"
                            ng-model="value"
                            ng-minlength="{{Question.min}}"
                            ng-maxlength="{{Question.max}}"
                            ng-keypress="enter($event,value)"
                            placeholder="{{Question.placeholder}}"
                            ng-pattern="{{Question.pattern}}" />
                            <!-- <div class="custom-error institution" ng-show="addForm.institutionName.$error.minlength || addForm.institutionName.$error.maxlength">
                                Minimum {{Question.min}} znaków
                            </div> -->
                            <!-- <div class="custom-error institution" ng-show="addForm.institutionName.$error.maxlength">
                                   Max {{Question.max}} znaków
                            </div> -->
                            <div class="custom-error institution" ng-show="addForm.institutionName.$error.pattern || addForm.institutionName.$error.maxlength || addForm.institutionName.$error.minlength">
                                   {{Question.copy}}
                            </div>
                    </div>
                </form>
            </div>
            <p>
                {{lang.sec101.title2}}
            </p>
            <a href="rejestracja_10edit.html" class="btn btn-lg btn-default bt_edit" type="button">
                {{lang.sec101.title3}}<span class="btn_bg_img"></span>
            </a>
        </div>
        <div class="col-md-12 noPad" ng-repeat="sek in formOut">
            <h3 class="daneHeading">{{sek.name}}</h3>
            <div class="row">
                <div class="col-md-12" >
                    <div class="col-md-4 col-sm-6 boxContent3 boxes" ng-repeat="row in sek.data">
                        <span class="leftBoxContrnt">{{row.shortAsk}}</h4></span>
                        <h4 class="rightBoxContrnt">{{row.value}}</h4>
                    </div>
                </div>
            </div>
        </div>

        <!-- <div class="row col-md-12" >
            <h3 class="daneHeading">Hasło</h3>
        </div>
        <div class="row">
            <div class="col-md-12 " >
                <div class="col-md-4 col-sm-6 boxContent3">
                    <span class="leftBoxContrnt">Test</h4></span><span class="blueTxt">&nbsp;*</span>
                    <h4 class="rightBoxContrnt">Test</h4>
                </div>
            </div>
        </div> -->
</div>

我的控制员:

var app = angular.module('app', ['ngAnimate', 'ngCookies', 'ui.bootstrap', 'ngSanitize', 'nya.bootstrap.select', 'jackrabbitsgroup.angular-slider-directive']);

// var rej10_Controller = function($scope, $http, $cookieStore, $timeout, limitToFilter) {
app.controller('rej10_Controller', ['$scope', '$http', '$cookieStore', '$sce', '$timeout', 'limitToFilter',
    function($scope, $http, $cookieStore, $sce, $timeout, limitToFilter) {

        var view = 10,
            arr,
            data,
            counterSection = 0,
            counterAsk = 0;

        $scope.opts = {};
        $scope.slider_id = 'my-slider';
        $scope.opts = {
            'num_handles': 1,
            'user_values': [0, 1],
            'slider_min': 0,
            'slider_max': 1,
            'precision': 0,
        };

        /* language */
        if (!$cookieStore.get('lang'))
            $cookieStore.put('lang', 'pl');


        var lang = $cookieStore.get('lang');
        $scope.language = lang;

        $scope.setLang = function(data) {
            $cookieStore.put('lang', data);

            $http.get('../widoki/json/lang/' + data + '/rej_' + view + '.json').
            success(function(data, status, headers, config) {
                $scope.lang = data;
                $scope.Question = $scope.lang.formIn.sekcja[counterSection].data[counterAsk];
                console.log($scope.lang);
            }).
            error(function(data, status, headers, config) {
                console.log('error load json');
            });

            $scope.language = data;
        };
        /* get language pack */
        $http({
            method: 'GET',
            url: '../widoki/json/lang/' + lang + '/rej_' + view + '.json'
        }).
        success(function(data, status, headers, config) {
            $scope.lang = data;
            $scope.Question = $scope.lang.formIn[counterSection].data[counterAsk];
            $scope.langLen = $scope.lang.formIn.length;

        }).error(function(data, status, headers, config) {
            console.log('error load json');
        });

        /* dynamic form */

        $scope.enter = function(ev, d) {
            if (ev.which == 13) {
                $scope.addData(d);
            }
        };
        $scope.addData = function(data) {
            var newData = {};
            /* latamy po id sekcji i po id pytania */
            if (!$scope.formOut) {
                $scope.formOut = [];
            }


            /* budowanie modelu danych wychodzcych */
            newData = {
                sekcja: counterSection,
                name: $scope.lang.formIn[counterSection].name,
                data: []
            };
                console.log(name);
            if (!$scope.formOut[counterSection]) {
                $scope.formOut.push(newData);
            }
            $scope.formOut[counterSection].data.push($scope.Question);
            $scope.formOut[counterSection].data[counterAsk].value = data;

            counterAsk++;

            // zmieniamy sekcje
            if (counterAsk == $scope.lang.formIn[counterSection].data.length) {
                counterAsk = 0;
                counterSection++;
            }
            if (counterSection == $scope.lang.formIn.length) {
                $scope.Question = '';
                /* zrobic ukrycie pola z formularza */
            } else {
                $scope.Question = $scope.lang.formIn[counterSection].data[counterAsk];
            }

            /* konwertowanie do jsona */
            //var Json = angular.toJson($scope.formOut);
            //console.log(Json);
        };
        $scope.submit = function() {
            alert('form sent'); /* wysĹanie formularza */
        };
        $scope.getClass = function(b) {
            return b.toString();
        };

    }
]);
app.directive('ngEnter', function() {
    return function(scope, element, attrs) {
        element.bind("keydown keypress", function(event) {
            if (event.which === 13) {
                scope.$apply(function() {
                    scope.$eval(attrs.ngEnter);
                });

                event.preventDefault();
            }
        });
    };
});
app.config(['$tooltipProvider',
    function($tooltipProvider) {
        $tooltipProvider.setTriggers({
            'mouseenter': 'mouseleave',
            'click': 'click',
            'focus': 'blur',
            'never': 'mouseleave',
            'show': 'hide'
        });
    }
]);
var ValidSubmit = ['$parse',
    function($parse) {
        return {
            compile: function compile(tElement, tAttrs, transclude) {
                return {
                    post: function postLink(scope, element, iAttrs, controller) {
                        var form = element.controller('form');
                        form.$submitted = false;
                        var fn = $parse(iAttrs.validSubmit);
                        element.on('submit', function(event) {
                            scope.$apply(function() {
                                element.addClass('ng-submitted');
                                form.$submitted = true;
                                if (form.$valid) {
                                    fn(scope, {
                                        $event: event
                                    });
                                }
                            });
                        });
                        scope.$watch(function() {
                            return form.$valid
                        }, function(isValid) {
                            if (form.$submitted == false)
                                return;
                            if (isValid) {
                                element.removeClass('has-error').addClass('has-success');
                            } else {
                                element.removeClass('has-success');
                                element.addClass('has-error');
                            }
                        });
                    }
                };
            }
        };
    }
];
app.directive('validSubmit', ValidSubmit);

我无法弄清楚问题是什么。 提前谢谢。

1 个答案:

答案 0 :(得分:1)

尝试将ngClick按钮处理程序更改为ngSubmit表单处理程序并将其连接起来。你没有说出你正在使用的浏览器,但是大多数浏览器都会在ENTER按键上自动提交表单,除非你捕获它(你不是这样)。点击按钮不会这样做。