angularjs - Angular $watch: TypeError: Cannot read property of undefined -
i have $watch
applies filter date format:
$scope.$watch(function() { return pn.myobj.birthdate; },function(n,o){ var dateview = $filter('date')(pn.myobj.birthdate,'yyyy-mm-dd'); pn.myobj.birthdate = dateview; });
the first time load page read in console:
typeerror: cannot read property 'birthdate' of undefined
how can prevent problems arising in $watch
when object undefined
?
your object isn't initialize @ time $watch function called. check exists before attempting access 1 of it's properties.
$scope.$watch(function() { return pn && pn.myobj && pn.myobj.birthdate; }
Comments
Post a Comment