Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angularjs - Cannot read property 'split' of undefined

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?

like image 654
Arun Avatar asked Jun 05 '26 19:06

Arun


1 Answers

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;
                }

            }]);
like image 71
Mark Coleman Avatar answered Jun 08 '26 10:06

Mark Coleman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!