简体   繁体   English

无法弄清楚如何修改角度指令

[英]Unable to figure out how to modify angular directive

What i'm trying to do is on click of a button i'm trying to give the user the option to select one of the 2 images displayed and make the selected image draggable. 我想做的是单击一个按钮,我试图为用户提供选择显示的2张图像之一并使所选图像可拖动的选项。 My code shows 2 images and 1 is draggable. 我的代码显示2张图片,其中1张可拖动。 I'm not able to figure out how to add the feature to let user select one of the 2 images and den make that selected image draggable 我无法弄清楚如何添加功能以让用户选择2张图像之一,并使该图像可拖动

Here is my html code 这是我的HTML代码

    <div ng-app="test">
    <button name="Add" type="buton" ng-click="showMarker = true">Show List</button>
<hello>

</hello>
</div>

Here is the javascript code 这是JavaScript代码

    angular.module('test', []).directive('hello', function() {
    return {
        restrict : 'E',
        template : '<div style="position: relative" ng-show="showMarker"><img id="mr" src="http://www.mbs.edu/i/gmap_marker_default.gif" /><img src="http://www.hypercosm.com/images/icons/add_marker_icon2.gif" /></div>',
        replace: true,
        link : function(scope, element, attrs) {
                 $("#mr").draggable();
        }
    };
})

Here is the jsfiddle 这是jsfiddle

Some suggestions first. 首先提出一些建议。 Unless absolutely necessary don't create custom components / directives. 除非绝对必要,否则不要创建自定义组件/指令。 Generally its a tendency to create custom components because people still think in JQuery or DOM manipulation. 通常,这是创建自定义组件的趋势,因为人们仍然在考虑JQuery或DOM操作。 Think data first and then think how data + data-binding is going to modify your view. 首先考虑数据,然后考虑数据+数据绑定将如何修改您的视图。 The reason I am putting this forward is because of your hello directive which is just a template expander, isnt it? 我之所以提出这一点,是因为您的hello指令只是一个模板扩展器,不是吗? I am going to assume that it was just for demo purposes and not something you were planning to use in production. 我将假设它只是出于演示目的,而不是您计划在生产中使用的东西。 Lets come to what you want to achieve. 让我们来谈谈您想要实现的目标。

Two different markers which have a property of draggability but this property can be turned on or off.. 具有可拖动性的两个不同的标记,但是可以打开或关闭此属性。

Here is the directive that achieves that. 这是实现该目标的指令。

.directive('drag', function() {
    return {
        restrict : 'A',
        link : function(scope, elm, attrs) {
            $(elm).draggable({ containment: "window",disabled:false});
            scope.$watch(attrs.drag,function(val){
                if(val === true){
                    $(elm).draggable('option','disabled',false);
                }
                else{
                    $(elm).draggable('option','disabled',true);
                }
            });
        }
    };
})

Now once you write such a directive then adding a "drag"-ability is a piece of cake.. To any element add a drag="dragVariable" and it will become draggable based on whether dragVariable is true or false. 现在,一旦编写了这样的指令,那么添加“拖动”功能就很容易了。向任何元素添加一个drag =“ dragVariable”,根据dragVariable是true还是false它将变为可拖动。 Ofcourse this implementation is based on JQuery UI providing us the drag functionality. 当然,此实现基于JQuery UI,向我们提供了拖动功能。

Here is the final fiddle where we have setup two checkboxes which can be used to toggle the drag states of the two markers.. When drag1 is turned on you can drag marker 1 and when turned off the drag features stops working.. 这是最后的小提琴,我们在其中设置了两个复选框,可用于切换两个标记的拖动状态。.当drag1打开时,您可以拖动标记1;而当关闭时,拖动功能将停止工作。

http://jsfiddle.net/7fMCF/8/ http://jsfiddle.net/7fMCF/8/

Hope this helps. 希望这可以帮助。

if you change your id to classes it should work, since your id is only referencing the first image. 如果您将ID更改为类,则该ID应该有效,因为您的ID仅引用了第一张图片。

for instance if you use the class "image" and then append the one you want selected with "draggable" you should end up with this when you click the second image: 例如,如果您使用“图像”类,然后将要选择的类附加到“可拖动”类,则在单击第二个图像时应该以以下形式结束:

<div style="position: relative" ng-show="showMarker"><img class="image"  src="http://www.mbs.edu/i/gmap_marker_default.gif" /><img class="image draggable"  src="http://www.hypercosm.com/images/icons/add_marker_icon2.gif" /></div>

for this your selector would be 为此,您的选择器将是

$("image.draggable")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM