参考Javascript中AngularJs指令的生成
Reference the production of an AngularJs Directive in Javascript
这就是场景。
我的Html 中有指令
<div class="row xsResponse" id="productGrid">
<product product="product" ng-repeat="product in products"></product>
</div>
那个产品有点像
<div class="product">
<!-- ... -->
</div>
在同一个页面中,我有一个Javascript,它必须引用#productGrid中的所有.product(class),就像这个一样
document.querySelectorAll( '#productGrid .product' )).forEach( function( el ) {
//.......
}
问题是Javascript只能看到<product ...></product>
标记,而不能看到他的产品。有没有一种方法可以在ng重复之后调用Javascript?
很抱歉,如果这个问题看起来不那么干净,也很抱歉我的英语不好。谢谢你的建议。
编辑
为了便于说明,这里有javascript代码。
<script>
(function() {
var body = document.body,
dropArea = document.getElementById( 'drop-area' ),
droppableArr = [], dropAreaTimeout;
// initialize droppables
[].slice.call( document.querySelectorAll( '#drop-area .drop-area__item' )).forEach( function( el ) {
droppableArr.push( new Droppable( el, {
onDrop : function( instance, draggableEl ) {
// show checkmark inside the droppabe element
classie.add( instance.el, 'drop-feedback' );
clearTimeout( instance.checkmarkTimeout );
instance.checkmarkTimeout = setTimeout( function() {
classie.remove( instance.el, 'drop-feedback' );
}, 800 );
// ...
}
} ) );
} );
// initialize draggable(s)
[].slice.call(document.querySelectorAll( '#grid .product' )).forEach( function( el ) {
new Draggable( el, droppableArr, {
draggabilly : { containment: document.body },
onStart : function() {
// clear timeout: dropAreaTimeout (toggle drop area)
clearTimeout( dropAreaTimeout );
// show dropArea
classie.add( dropArea, 'show' );
},
onEnd : function( wasDropped ) {
var afterDropFn = function() {
// hide dropArea
classie.remove( dropArea, 'show' );
};
if( !wasDropped ) {
afterDropFn();
}
else {
// after some time hide drop area and remove class 'drag-active' from body
clearTimeout( dropAreaTimeout );
dropAreaTimeout = setTimeout( afterDropFn, 400 );
}
}
} );
} );
})();
</script>
当尝试混合jQuery和Angular时,这是一个常见的问题。jQuery代码将在angular添加元素之前运行。运行jQuery代码后添加的任何元素都不会得到处理。解决方案是自定义指令。一个简单的例子是:
.directive('product', function() {
return {
restrict: 'C',
link: function(scope, el, attr) {
//el is a jQuery object (assuming you've included jQuery)
//you can run your custom jQuery logic on the element here
};
})
该指令的链接函数将为每个具有class="product"的元素调用。
它在功能上相当于:
document.querySelectorAll( '.product' )).forEach( function( el ) {
//.......
});
除了作为一个指令,它将被调用,即使是后来添加的元素(只要它们是按角度添加的)。
相关文章:
- 一次显示一个隐藏指令-AngularJS
- 一次显示一个指令AngularJS
- 在同一页面上多次使用的自定义指令 (AngularJS) 中的验证
- 无法绑定到没有指令 + angularjs 标识符的控制器
- 值未传递给指令-AngularJS
- 测试依赖于另一个指令Angularjs的指令
- 根据指令angularjs 1.2分配控制器
- 有模板问题的指令.AngularJS
- 编译指令在指令 AngularJS 中
- 我可以将 templateUrl 传递给指令 - AngularJS
- 从子指令的父指令 [angularJS] 触发子指令中的函数
- 将服务注入指令 AngularJS 和数据绑定
- 在指令 AngularJS 中解析
- 从指令 AngularJS 给出高图表正确的参数
- 获取 JSON 数据并传递给指令 Angularjs
- 点击事件后的渲染模板指令 - AngularJs
- 如何从分页指令angularjs隐藏直接页面链接
- AngularJS-关闭tile并从ngRepeat指令AngularJS中的模型加载新视图
- 触发指令AngularJs的控制器作用域
- 将定义的对象传递给另一个自定义指令angularjs