aga*_*yan 16 javascript angularjs angularjs-directive
我是AngularJS newby.我正在尝试使用AngularJS指令的模板显示图像,并单击图像我想要将标记放置在图像上.我不知道为什么它不起作用.
第一个指令:
directive('hello', function() {
return {
template: '<img id="map" src="http://www.lonelyplanet.com/maps/asia/india/map_of_india.jpg" />',
link: function(scope, element, attrs) {
$('#map').click(
function(e) {
$('#marker').css('left', e.pageX).css('top', e.pageY).show();
}
);
},
};
});
Run Code Online (Sandbox Code Playgroud)
HTML代码
<hello>
<img id="marker" src="http://maps.google.com/mapfiles/ms/micons/blue.png" style="display: none; position: absolute;" />
</hello>
Run Code Online (Sandbox Code Playgroud)
Sam*_*num 36
其他一些可能的问题:
widgetForm并且restrict是,'E'或者'A'您应该使用<widget-form/>,而不是<widgetForm/>.Aru*_*hny 23
您缺少该restrict : 'E'选项,默认情况下restrict具有AC属性和类的值,在您使用该指令作为元素的情况下.
更新:根据评论
angular.module('test', []).directive('hello', function() {
return {
restrict : 'E',
template : '<div style="position: relative"><img id="map" src="http://www.lonelyplanet.com/maps/asia/india/map_of_india.jpg" /><img id="marker" src="http://maps.google.com/mapfiles/ms/micons/blue.png" style="display: none; position: absolute;" /></div>',
replace: true,
link : function(scope, element, attrs) {
$('#map').click(function(e) {
$('#marker').css('left', e.pageX).css('top', e.pageY)
.show();
});
}
};
});
Run Code Online (Sandbox Code Playgroud)
演示:小提琴