javascript - Scope variable error: Cannot read property ... of undefined -
in code have table , want fill form row clicked. table:
<table class="table table-striped"> <thead> <tr> <th>#</th> <th>project</th> <th>start</th> <th>end</th> </tr> </thead> <tbody> <tr ng-repeat="booking in mybookingslist" ng-click="bookingclicked(booking)"> <td>{{$index + 1}}</td> <td>{{booking.usersprojects.project.name}}</td> <td>{{booking.start | date:'yyyy-mm-dd hh:mm:ss' }}</td> <td>{{booking.end | date:'yyyy-mm-dd hh:mm:ss' }}</td> </tr> </tbody>
here function ng-click:
$scope.bookingclicked = function(booking){ $scope.newbooking = {}; $scope.newbooking.project = booking.usersprojects.project.name; $scope.newbooking.id = booking.id; $scope.newbooking.startdate = $filter('date')(booking.start, "yyyy-mm-dd"); $scope.newbooking.starttime = new date(booking.start); $scope.newbooking.enddate = $filter('date')(booking.end, "yyyy-mm-dd"); $scope.newbooking.endtime = new date(booking.end); };
the problem 2nd line
typeerror: cannot read property 'project' of undefined
so problem here? part of app takes data from , submits it. working fine , there have
$scope.newbooking = {}; //timepicker values must initialized $scope.newbooking.starttime = new date(); $scope.newbooking.endtime = new date();
here example booking object fed function:
{ "id": 50, "self": "http://localhost:8080/timetracker-backend/timetracker/booking/50", "start": 1434103331000, "end": 1434110531000, "usersprojects": { "id": 43, "self": "http://localhost:8080/timetracker-backend/timetracker/usersprojects/43", "user": { "id": 1, "self": "http://localhost:8080/timetracker-backend/timetracker/user/1", "name": "timetrackeradmin", "role": "admin" }, "project": { "id": 42, "self": "http://localhost:8080/timetracker-backend/timetracker/project/42", "name": "project four", "description": "description p4" } } }
this looks similar me??
in $scope.newbooking = {};
there no object name project showing console error, create empty object in answer as
$scope.newbooking.project = {};
it means
$scope.newbooking= { project : {} }
you line works above code, $scope.newbooking has or know above project object so, works fine
Comments
Post a Comment