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
1 Answer
Reset to default 5You 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
版权声明:本文标题:javascript - Angularjs - Cannot read property 'split' of undefined - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741330336a2372731.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论