admin管理员组

文章数量:1287933

I am trying to split the date entered using the text field. I want it in an array.

In html file

<%= text_field_tag :checkoutdate, params[:checkoutdate],:placeholder => 'Select Date', :class=>'form-control datepicker input-lg',:required=>true,"ng-model" => "checkoutdate"%>

    <p class='bg-danger' ng-show="isInvalidDate()">Check-out date cannot be lesser than check-in date</p>

In Controller js file

App.controller('validationController',['$scope', function($scope){
$scope.isInvalidDate = function(){
     var checkin = $scope.checkindate.split('-');
     var checkout = $scope.checkoutdate.split('-');

    if($scope.checkin[0]>$scope.checkout[0] || $scope.checkin[1]>$scope.checkout[1]){
        return true;
    }

}

and i get the error in console

Cannot read property 'split' of undefined

what am i doing wrong?

I am trying to split the date entered using the text field. I want it in an array.

In html file

<%= text_field_tag :checkoutdate, params[:checkoutdate],:placeholder => 'Select Date', :class=>'form-control datepicker input-lg',:required=>true,"ng-model" => "checkoutdate"%>

    <p class='bg-danger' ng-show="isInvalidDate()">Check-out date cannot be lesser than check-in date</p>

In Controller js file

App.controller('validationController',['$scope', function($scope){
$scope.isInvalidDate = function(){
     var checkin = $scope.checkindate.split('-');
     var checkout = $scope.checkoutdate.split('-');

    if($scope.checkin[0]>$scope.checkout[0] || $scope.checkin[1]>$scope.checkout[1]){
        return true;
    }

}

and i get the error in console

Cannot read property 'split' of undefined

what am i doing wrong?

Share Improve this question edited May 3, 2014 at 14:44 Arun asked May 3, 2014 at 14:22 ArunArun 1,5283 gold badges19 silver badges35 bronze badges 5
  • 1 When are you running the split? (my guess is there is no value or an invalid value as the model will bee undefined if it is not valid) – Mark Coleman Commented May 3, 2014 at 14:34
  • How do i run split after i enter value in input field? – Arun Commented May 3, 2014 at 14:35
  • When do you do the split? a $watch? ngClick? or? – Mark Coleman Commented May 3, 2014 at 14:36
  • i am new to angular,i have a model for input field called checkoutdate, so i am accessing it in controller and calling the split function. How do i call it after i have selected a date? – Arun Commented May 3, 2014 at 14:39
  • Please provide more code, I am not sure what or how you are doing this without seeing it. – Mark Coleman Commented May 3, 2014 at 14:41
Add a ment  | 

1 Answer 1

Reset to default 5

You are running into dirty checking when the value is not defined yet. Simply check to make sure the value is not undefined before you run your function.

App.controller('validationController', ['$scope',
        function ($scope) {
            $scope.isInvalidDate = function () {
                if($scope.checkindate === undefined || $scope.checkoutdate === undefined){
                    return false;
                }
                var checkin = $scope.checkindate.split('-');
                var checkout = $scope.checkoutdate.split('-');

                if ($scope.checkin[0] > $scope.checkout[0] || $scope.checkin[1] > $scope.checkout[1]) {
                    return true;
                }

            }]);

本文标签: javascriptAngularjsCannot read property 39split39 of undefinedStack Overflow