angularjs-bindng-click从jquery动态加载DOM
angularjs - bind ng-click for dynamically loaded DOM from jquery
我有一个ng控制器PostsListController来控制加载图像的ng-click
<div ng-controller='PostsListController'>
<ul id="wookmark_container">
<% @posts.each do |post|%>
<li>
<a href='#' ng-click="open()"> <img src="image_path" ></a>
</li>
<%end%>
</ul>
</div>
angular.module('myapp')
.controller('PostsListController', function($scope, $http){
$scope.open = function(postId){
};
};
});
<script type="text/javascript">
(function ($) {
var container = '#wookmark_container',
$container = $(container),
tileCount = 30,
$window = $(window),
$document = $(document),
wookmark;
// Initialize Wookmark
wookmark = new Wookmark(container, {
offset: 5, // Optional, the distance between grid items
outerOffset: 10, // Optional, the distance to the containers border
itemWidth: 260 // Optional, the width of a grid item
});
function onScroll() {
var winHeight = window.innerHeight ? window.innerHeight : $window.height(), // iphone fix
closeToBottom = ($window.scrollTop() + winHeight > $document.height() - 100);
if (closeToBottom) {
var $items = $('li', $container),
$firstTen = $items.slice(0, 10).clone().css('opacity', 0);
$container.append($firstTen);
wookmark.initItems();
wookmark.layout(true, function () {
// Fade in items after layout
setTimeout(function() {
$firstTen.css('opacity', 1);
}, 300);
});
}
};
// Capture scroll event.
$window.bind('scroll.wookmark', onScroll);
})(jQuery);
</script>
更新的脚本
html部分添加scroll
<ul scroll id="wookmark_container" style="position: relative">
angular.module('bidwars').directive("scroll", function ($window) {
return function(scope, element, attrs) {
angular.element($window).bind("scroll", function() {
scope.$apply(function(){
// write your jquery logic here
var container = '#wookmark_container',
$container = $(container),
tileCount = 30,
$window = $(window),
$document = $(document),
wookmark;
// Initialize Wookmark
wookmark = new Wookmark(container, {
offset: 5, // Optional, the distance between grid items
outerOffset: 10, // Optional, the distance to the containers border
itemWidth: 260 // Optional, the width of a grid item
});
var winHeight = window.innerHeight ? window.innerHeight : $window.height(), // iphone fix
closeToBottom = ($window.scrollTop() + winHeight > $document.height() - 100);
if (closeToBottom) {
// Get the first then items from the grid, clone them, and add them to the bottom of the grid
var $items = $('li', $container),
$firstTen = $items.slice(0, 10).clone().css('opacity', 0);
$container.append($firstTen);
wookmark.initItems();
wookmark.layout(true, function () {
// Fade in items after layout
setTimeout(function() {
$firstTen.css('opacity', 1);
}, 300);
});
}
});
});
};
});
(function ($) {
var container = '#wookmark_container',
$container = $(container),
tileCount = 30,
$window = $(window),
$document = $(document),
wookmark;
// Initialize Wookmark
wookmark = new Wookmark(container, {
offset: 5, // Optional, the distance between grid items
outerOffset: 10, // Optional, the distance to the containers border
itemWidth: 260 // Optional, the width of a grid item
});
})(jQuery);
它适用于在初始页面加载时加载的所有图像。但是当我使用jquery将更多的图像加载到wookmark_container
时。ng-click
不适用于jquery中新加载的图像。我的猜测是ng控制器没有意识到这些来自jquery的新加载图像。
我如何让ng控制器现在从jquery加载这些新的?如何使动态加载的图像的ng-click
事件工作?
在控制器中,将更新代码包装在$apply
中,就像这样-
$scope.$apply(function () {
// update your html by jQuery
});
这将确保AngularJS引擎知道DOM的任何更改。
阅读本文了解更多详细信息。
你可以做这样的事情-
app.directive("scroll", function ($window) {
return function(scope, element, attrs) {
angular.element($window).bind("scroll", function() {
$scope.$apply(function(){
// write your jquery logic here
});
});
};
});
我还没有在你的代码中尝试过,但应该可以。
相关文章:
- 加载dom后禁用媒体查询
- angularjs-bindng-click从jquery动态加载DOM
- 向使用jQuery加载DOM后添加的字段添加不受限制的文本输入DatePickers
- chrome扩展在加载dom之前注入动态css
- 删除在加载dom之后创建的动态元素
- 在完全加载 DOM 之前更改 DOM 的方法
- 加载 DOM 并使用 .Net 在服务器端执行 javascript
- 加载 DOM 后无法读取动态创建的复选框的属性
- getElementById 在加载 dom 后返回未定义
- 在完全加载DOM之前隐藏父元素
- jQuery在完全加载DOM后严格执行操作
- 使用ng视图属性时不加载DOM元素
- 如何在加载DOM元素后立即获取该元素
- 替换jquery's仅在加载DOM功能后运行
- JavaScript模糊事件不适用于加载DOM后添加的项
- 阻止加载DOM对象
- 如果在加载DOM时找不到javascript库,我该如何动态更改它
- 加载DOM后,如何使用jQuery从Select输入中获取值
- 如何在加载DOM之前开始加载iframe
- 加载dom后,使用jquery更改js代码