简体   繁体   English

无法使用 Angular.js 获取输入字段值

[英]Cannot get the input field value using Angular.js

I am unable to find the input field values using Angular.js.我无法使用 Angular.js 找到输入字段值。 I am providing my code below:我在下面提供我的代码:

<div class="input-group bmargindiv1 col-md-12">
    <span class="input-group-addon ndrftextwidth text-right" style="width:180px">From Time :</span>
    <input type="text" name="time" id="time" class="form-control oditek-form" placeholder="E.G-9.00AM-10.00AM" ng-model="time" ng-keypress="clearField('time');" maxlength="30">
</div>
<div class="input-group bmargindiv1 col-md-12">
    <span class="input-group-addon ndrftextwidth text-right" style="width:180px">To Time :</span>
    <input type="text" name="time1" id="time1" class="form-control oditek-form" placeholder="E.G-9.00AM-10.00AM" ng-model="time1" ng-keypress="clearField('time1');" maxlength="30">
</div>

Here I am integrating the jquery-timepicker and the script is given below.这里我集成了jquery-timepicker ,脚本如下。

$(function() {
    $('#time').timepicker();
    $('#time1').timepicker();
});

My problem is, after selecting the time value when I am trying to fetch those value using one ng-click event, it's showing undefined.我的问题是,当我尝试使用一个ng-click事件获取这些值时选择了时间值后,它显示未定义。

$scope.addTimeDetails = function() {
    console.log('times', $scope.time, $scope.time1);
}

Mixing AngularJS with jQuery libraries is bad solution.将 AngularJS 与 jQuery 库混合使用是一个糟糕的解决方案。 Problem is next: You are defining ng-model on input and then initializing jQuery timepicker which makes a problem.接下来的问题是:您在输入上定义 ng-model,然后初始化 jQuery timepicker,这会产生问题。 You should use some third party angularjs library for datepicker or build your own directive.您应该为 datepicker 使用一些第三方 angularjs 库或构建您自己的指令。 This one is the similar to the jQuery timepicker: angular-jquery-timepicker这个类似于 jQuery 时间选择器: angular-jquery-timepicker

This won't work, since jquery-datepicker modifies the DOM.这行不通,因为 jquery-datepicker 修改了 DOM。 You can try using angular-datepicker or angular UI bootstrap您可以尝试使用angular-datepickerangular UI bootstrap

ngModels should always have their own object and not be scope properties: ngModels 应该总是有自己的对象而不是范围属性:

<input type="text" ng-model="formModel.time">

And in your controller:在您的控制器中:

$scope.formModel = {};

You will then be able to access the value with:然后,您将能够通过以下方式访问该值:

$scope.formModel.time;

jquery timepicker may not fire the keypress event. jQuery的timepicker可能不会触发keypress事件。

try it with ng-changeng-change试试

<input type="text" name="time" id="time" class="form-control oditek-form" placeholder="E.G-9.00AM-10.00AM" ng-model="time" ng-change="clearField('time');"  maxlength="30" >

When you use third party library in angular, recommended approach is that you create a directive as following:在 angular 中使用第三方库时,推荐的方法是创建如下指令:

angular.module('app', [ ])
    .directive('timePicker', [function () {
        return {
        restrict: 'A',
        require: 'ngModel',
        scope: {
            model: '=ngModel'
        },
        link: function ($scope, element, attrs, ngModelCtrl) {

            element.timepicker();

            element.on('changeTime', function () {
                $scope.$apply(function () {
                    $scope.model = element.val();
                });
            });       
        }
    }
}]);

and use from as:并使用从作为:

<input type="text" ng-model="time" time-picker="" />
<input type="text" ng-model="time1" time-picker="" />

Recommended resource: thinking-in-angularjs-if-i-have-a-jquery-background推荐资源: think-in-angularjs-if-i-have-a-jquery-background

<script>
$(function() {
    $('#time').timepicker({change: function(time) {
        // the input field
        $scope.time = time;
        if (!$scope.$$phase) $scope.$apply();
    }});
    $('#time1').timepicker({change: function(time) {
        // the input field
        $scope.time1 = time;
        if (!$scope.$$phase) $scope.$apply();
    }});
});
<script>   

You need to put this code in angular js controller to set values in model instead of putting in HTML.您需要将此代码放在 angular js 控制器中以在模型中设置值而不是放入 HTML。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM