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

Popular posts from this blog

yii2 - Yii 2 Running a Cron in the basic template -

asp.net - 'System.Web.HttpContext' does not contain a definition for 'GetOwinContext' Mystery -

mercurial graft feature, can it copy? -