Angularjs 更新所有新元素的指令作用域表达式
我有一个指令,它是html元素的前置。当用户单击以将它们放置在屏幕上时,它们都有位置表达式,但当添加每个新的位置表达式时,位置表达式将添加到新元素中。我只希望它添加到新元素中,而不是DOM中已经存在的元素Angularjs 更新所有新元素的指令作用域表达式,angularjs,angularjs-directive,Angularjs,Angularjs Directive,我有一个指令,它是html元素的前置。当用户单击以将它们放置在屏幕上时,它们都有位置表达式,但当添加每个新的位置表达式时,位置表达式将添加到新元素中。我只希望它添加到新元素中,而不是DOM中已经存在的元素 .directive("addbuttons", function($compile, PositionService, $timeout){ return { restrict: 'A', scope: true, lin
.directive("addbuttons",
function($compile, PositionService, $timeout){
return {
restrict: 'A',
scope: true,
link: function ($scope, $element, attrs) {
$timeout(function () {
var square = $element[0], posX = 0, posY = 0;
ionic.onGesture('tap drag dragend', function (e) {
e.gesture.srcEvent.preventDefault();
e.gesture.preventDefault();
switch (e.type) {
case 'tap':
posX = event.gesture.touches[0].pageY
$scope.posX = posX;
break;
}
}, $element[0]);
});
$element.bind("click", function(){
angular.element(document.getElementById('tagContainer')).prepend($compile("<div class='square' style='left:{{posX}}px; top:{{posY}}px' data-ion-pinch></div>")($scope));
});
}
};
});
指令(“添加按钮”,
函数($compile,PositionService,$timeout){
返回{
限制:“A”,
范围:正确,
链接:函数($scope$element,attrs){
$timeout(函数(){
var square=$element[0],posX=0,posY=0;
IONAL.onGesture('轻敲拖动拖动',功能(e){
e、 srcEvent.preventDefault();
e、 preventDefault();
开关(e型){
案例“tap”:
posX=事件。手势。触摸[0]。页面
$scope.posX=posX;
打破
}
},$element[0]);
});
$element.bind(“单击”,函数(){
元素(document.getElementById('tagContainer')).prepend($compile(“”($scope));
});
}
};
});
我不知道你所有的代码,但是你可以用$element代替。bind。。。这是一种更具角度的方式。我使用ionic来确定屏幕上点击的X&Y位置,并将该位置添加到元素中,同时创建一个新元素。因此,当用户单击除创建的元素之外的任何其他位置时,新元素将添加到屏幕上,并具有自己的X&Y位置,但此位置将添加到旧元素