【发布时间】:2018-09-03 08:31:15
【问题描述】:
我想绑定rect的坐标x,y。(记录为“坐标”)。并拖动矩形,希望记录(坐标x,y)会同步变化。
以下是部分代码。(完整代码jsbin)
html
<tr ng-repeat="item in data">
<td>({{ item.x }}, {{ item.y }})</td>
<td>{{ item.width }}</td>
<td>{{ item.height }}</td>
</tr>
JS
$scope.data = [{ 'x': 30, 'y': 50, 'width': 90, 'height': 70 }];
var drag = d3.drag()
.on('drag', function (d) {
d3.select(this)
.attr('x', d.x = d3.event.x)
.attr('y', d.y = d3.event.y)
})
.on('end', function (d) {
//update coordinate x ,y to array
arrayNum = this.id;
$scope.data.splice(arrayNum, 1, { 'x': d.x, 'y': d.y, 'width': d.width, 'height': d.height });
console.log($scope.data);
});
我还有 $scope.data.splice 来更新数组。它确实更新了 $scope.data。 但它不适用于浏览器视图。我该如何修改?或者我可以参考什么?非常感谢!
【问题讨论】:
标签: angularjs d3.js drag-and-drop angularjs-ng-model ng-bind