我有一个div,它是可拖动的元素。它应该在拖动时放在组件上,我们应该在组件上显示数字。我附上了屏幕截图。
我正在使用angular指令拖放,这是下面的代码。我只是想知道我能够从指令中触发showLineEffect
方法,但是dropProperties
在UI中并未得到更新,但是在控制台中它在拖动时显示了更新的值。
我尝试了ng-mouseover
事件,但是当我们持有和设置元素时,它不起作用。请让我知道是否还有其他解决方法。
控制器:
var vm= this;
vm.dropProperties = {
showLine: false,
indentLeft: 0,
indentRight: 0,
totalWidth: 10,
totalHeight: 32,
startMonth: 0
};
vm.showLineEffect = function (event) {
vm.dropProperties.showLine = true;
vm.dropProperties.startMonth = Math.round((event.clientX - 160) / 48);
console.log($ctrl.dropProperties +"changed");
}
指令:
app.directive('dragOppSol',
function dragOppSol() {
return {
restrict: 'A',
link: function (scope, element, attrs) {
element.prop('draggable', true);
element.on('dragstart', function (event) {
console.log("drag started");
//event.dataTransfer.setData('text', event.target.id)
});
}
};
}
);
app.directive('dropOppSol',
function dropOppSol() {
return {
restrict: 'A',
link: function (scope, element, attrs) {
element.on('dragover', function (event) {
event.preventDefault();
//console.log("test change" + scope.ctrl.dropProperties.startMonth);
scope.ctrl.showLineEffect(event);
// Implementation of CSS
});
element.on('drop', function (event) {
event.preventDefault();
});
}
};
});
答案 0 :(得分:1)
在scope.ctrl.showLineEffect(event);之后添加public GameObject MyCanvas;
void Start(){
if(MyCanvas.GetComponentInChildren<Button>() != null){
Debug.Log("Button found");
else {
Debug.Log("Button not found");
}
}
;或在vm.showLineEffect中。
$ scope。$ apply ~~告诉它应该检查并更新所有html绑定。
任何ng-smth,例如ng-click,ng-mouseover,ng-keydown ...都可以:
$scope.$apply()