作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想创建一个自定义指令(我们称之为 MyDragSource),它可以通过调用作用域上的函数来获取其“拖动上下文”,如下所示:
<div my-drag-source drag-context="getDragContext">
在本例中,getDragContext 是在范围上定义的函数:
$scope.getDragContext = function() { // return drag source }
我希望在拖动操作开始时延迟调用 getDragContext,而不是预先评估。我怎样才能做到这一点?
在我的自定义指令中,我尝试了以下操作:
$scope.$eval($attrs.dragContext);
但是,这并没有产生预期的结果。
最佳答案
将函数作为指令的属性传递:
<div my-drag-source drag-context="getDragContext()" >
在指令中使用隔离范围,然后导入 dragContext
使用&
,它将范围属性定义为一个函数,该函数返回评估父范围中属性内容的结果(在本例中为 $scope.$parent.$eval(attrs.dragContext)
):
scope: {
dragContext: '&'
}
然后你就可以在 link
中随时调用该函数功能:
$scope.dragContext()
如果您传递的方法不带括号,您仍然可以从 link
调用它函数,您只需要通过添加另一组括号来调用该函数:
$scope.dragContext()()
关于javascript - Angular : creating a custom directive that can call a scope function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28553138/
我是一名优秀的程序员,十分优秀!