[英]what is the best way to run jquery code in angularjs?
I want to run some jquery code when DOM Ready. 我想在DOM Ready时运行一些jquery代码。 I used
我用了
angular.element(document).ready(function() {
$(".hover-brown").mouseover(function(e) {
if ($(e.target).closest('.hover-yellow').length == 0) {
$(this).not(".hover-yellow").css("background", "#e8e8e8");
}
}).mouseout(function() {
$(this).not(".hover-yellow").css("background", "white");
});
$(".hover-yellow").hover(function() {
$(this).css("background", "#e8e8e8");
}, function() {
$(this).css("background", "white");
});
});
and tried window.load
as well but it runs before Dom is ready ie it does not find the elements when this function run. 并尝试了
window.load
,但它在Dom准备好之前运行,即它在此函数运行时找不到元素。
Note: the elements are the <li>
elements with class hover-brown rendered in view using ng-repeat directive. 注意:元素是
<li>
元素,使用ng-repeat指令在视图中呈现类hover-brown。
You did some conceptual errors. 你做了一些概念错误。 When you're using angular js you should avoid "jquery pattern" to assign event to the DOM.
当你使用角度js时,你应该避免使用“jquery pattern”来为DOM分配事件。 You shouldi instead use directive directly on DOM element.
你应该直接在DOM元素上使用指令。
For example, if you need browser triggers some custom code on mouseover you should create a function in your controller and assign to an element via ngMouseover directive ( https://docs.angularjs.org/api/ng/directive/ngMouseover ). 例如,如果您需要浏览器触发鼠标悬停时的一些自定义代码,您应该在控制器中创建一个函数,并通过ngMouseover指令( https://docs.angularjs.org/api/ng/directive/ngMouseover )分配给一个元素。
The same approach would be used for alter style of your nodes. 相同的方法将用于改变节点的样式。 You should use some variables that represents states (for example active) and bind css definition to this variables.
您应该使用一些表示状态的变量(例如活动)并将css定义绑定到此变量。
You can take a look here: http://codepen.io/anon/pen/gpBEgR 你可以看看这里: http : //codepen.io/anon/pen/gpBEgR
angular.module('demo', []).controller('MyController', function($scope) {
$scope.over = function() {
$scope.style['backgroundColor'] = 'yellow';
}
$scope.out = function() {
$scope.style['backgroundColor'] = 'green';
}
$scope.style = {
backgroundColor: 'green'
};
});
There is no problem with using jquery in angularjs, but you should use it within the context, like if it's related to a page use it in the controller. 在angularjs中使用jquery没有问题,但你应该在上下文中使用它,就好像它与页面相关,在控制器中使用它。 If it's related to a directive use it in directive's link or controller functions.
如果它与指令有关,则在指令的链接或控制器函数中使用它。 That way, you will be sure that the document is loaded.
这样,您将确保加载文档。
angular.element
is not set until bindJQuery()
in Angular is executed (Angular 1.4.1, Line 28288): angular.element
未设置直到bindJQuery()
在角被执行(角1.4.1,28288线):
jqLite(document).ready(function() {
angularInit(document, bootstrap);
});
So angular.element
is not availabe before document is ready. 因此,在文档准备好之前,
angular.element
不可用。
You should prevent the jQuery
way to manipulate DOM structures. 您应该阻止
jQuery
方式来操纵DOM结构。 If you do want to do it, move the operation to the directive link
functions . 如果您确实想这样做,请将操作移至指令
link
功能 。 That means, move your code $(".hover-brown").mouseover(function(e) {...
to some directive link
function. 这意味着,移动你的代码
$(".hover-brown").mouseover(function(e) {...
到某个指令link
函数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.