在我的代码中我有一个表格,我想用我点击的行填写表格。 表:
<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>
这是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);
};
问题是我得到的第二行
TypeError: Cannot read property 'project' of undefined
那么这里有什么问题?应用程序的另一部分从中获取数据并提交它。这工作正常,我有
$scope.newBooking = {};
//timepicker values must be initialized
$scope.newBooking.startTime = new Date();
$scope.newBooking.endTime = new Date();
以下是预订对象的示例:
{
"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"
}
}
}
这看起来与我类似?
答案 0 :(得分:1)
$ scope.newBooking = {};
没有对象名称项目因此显示控制台错误,您在答案中创建了一个空对象
$scope.newBooking.project = {};
意味着
$scope.newBooking= {
project : {}
}
你的行如上代码所示,现在$ scope.newBooking已经或知道项目对象以上,所以,它工作正常
答案 1 :(得分:0)
无意中我找到了解决方案。如果我添加$scope.newBooking.project = {};
所以功能现在是
$scope.bookingClicked = function(booking){
$scope.newBooking = {};
$scope.newBooking.project = {};
$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);
};
但是,无论谁能告诉我为什么这只是使用这个额外的行将被接受。