[英]jquery in angular js directive
Hi I have a problem to run function writing in jquery in angular. 嗨,我有一个问题,在角度jquery中运行函数编写。 I got erreor function is not defined.
我的erreor函数没有定义。 I have a write smiliar fuction and working fine but this function doesn't work.
我有一个写熟的功能和工作正常但这个功能不起作用。 This is function inside html file and work correct link to fiddle http://jsfiddle.net/ADukg/10472/
这是html文件中的函数和工作正确链接到小提琴http://jsfiddle.net/ADukg/10472/
<script type="text/javascript">
// dragabble function
$(function($) {
var panelList = $('#draggablePanelList');
panelList.sortable({
// Only make the .panel-heading child elements support dragging.
// Omit this to make then entire <li>...</li> draggable.
handle: '.panel-heading',
update: function() {
$('.panel', panelList).each(function(index, elem) {
var $listItem = $(elem),
newIndex = $listItem.index();
// Persist the new indices.
});
}
});
});
// get parent id selected by radio button
var parentId;
$(document).ready(function() {
$('input:radio[name=edit-panel]').change(function() {
parentId = $(this).parent().parent().attr("id");
});
});
// edit selected element
function editFunction() {
console.log(parentId);
if (!$("input[name='edit-panel']:checked").val()) {
return false;
} else {
var newInputSize = document.getElementById("newSizeEditor");
var newSize = newInputSize.options[newInputSize.selectedIndex].value;
$("#"+parentId).removeClass().addClass(newSize);
}
}
</script>
And this is my directive: 这是我的指示:
module.directive('dashboard', function() {
return {
restrict: 'A',
link: function (scope, element, attrs) {
$(function() {
var panelList = $('#draggablePanelList');
panelList.sortable({
handle: '.panel-heading',
update: function() {
$('.panel', panelList).each(function(index, elem) {
var $listItem = $(elem),
newIndex = $listItem.index();
});
}
});
});
scope.editFunction = function() {
// console.log(parentId);
if (!$("input[name='edit-panel']:checked").val()) {
return false;
} else {
var newInputSize = document.getElementById("newSizeEditor");
var newSize = newInputSize.options[newInputSize.selectedIndex].value;
$("#"+parentId).removeClass().addClass(newSize);
}
var parentId;
angular.element(document).ready(function () {
$('input:radio[name=edit-panel]').change(function() {
parentId = $(this).parent().parent().attr("id");
});
});
}
}
}
})
; ;
How did you implement this directive 'dashboard' in html ? 你是如何在html中实现这个指令'dashboard'的?
If you need to use function editFunction on the view, bind it to scope. 如果需要在视图上使用函数editFunction,请将其绑定到范围。
scope.editFunction = function(){...}
Give <input>
an ng-model="loadInputObj.val"
, and use angular directive option to pass object in, 给
<input>
一个ng-model="loadInputObj.val"
,并使用angular directive选项传入对象,
maybe you don't need jQuery, it will help you to prevent some bugs. 也许你不需要jQuery,它会帮助你防止一些错误。
scope: {
loadInputObj: '='
},
link: function (scope, ...) {
// use scope.loadInputObj.val here
}
ref: https://docs.angularjs.org/guide/directive 参考: https : //docs.angularjs.org/guide/directive
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.