angularjs - 我可以将匿名函数或代码作为指令中的属性吗?

标签 angularjs angularjs-directive anonymous-function

我可以做这样的事情:

<div set-height-to="function(){$(this).css($(".sth-elem").height()}"></div>

!!!我不想只设置高度,但事情更复杂,比如观察元素的 CSS 样式并通过一些偏移、计算等设置该元素的样式。

我将在指令内部有一个链接函数,并在某些地方调用输出函数,以使我的指令更加灵活 如何将匿名函数而不是 Controller 函数或某些 JavaScript 代码绑定(bind)为指令中的属性?

最佳答案

在你的标记中我会这样做:

<div set-height-to=".sth-elem"></div>

并在您的指令中使用此属性将元素的高度设置为您传入的选择器的高度。

在指令的链接函数中,您可以执行类似的操作(语法未验证)

element.css('height', $(attr.setHeightTo).height());

关于angularjs - 我可以将匿名函数或代码作为指令中的属性吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23473534/

相关文章:

javascript - AngularJS - 是否可以使用 ng-repeat 呈现 HTML 值?

javascript - D3 : replacing anonymous function with a named one

postgresql - 匿名函数postgresql的返回值

AngularJS : How does the HTML compiler arrange the order for compiling?

javascript - 在 JavaScript 事件代码中对回调和参数使用匿名函数而不是命名函数有什么好处?

javascript - 使用两个单选按钮和一个表单合并两个表单

javascript - Uncaught Error : [$injector:cdep] Circular dependency found: $templateRequest <- $animate <- cfpLoadingBar <- $http <- $templateRequest <- $compile

javascript - AngularUI Accordion 切换范围变量

javascript - 在 ui-router TemplateURL 中运行脚本

javascript - Angular 错误 : [ng:area] and [$injector:unpr] when import ngUpload