【问题标题】:How to target dynamic generated id {{element}} inside angular Directive如何在角度指令中定位动态生成的 id {{element}}
【发布时间】:2014-12-16 10:14:01
【问题描述】:

我有一个要求,我创建了如下指令:

app.directive('popOver', function ($compile) {
    return {
        restrict: "A",
        link: function (scope, element, attrs) {
            // get template
            var templateId = element.data('popover-template');
            var templateHtml = $('#' + templateId).html();
            // compile content and class
            var popover_content = $compile(templateHtml)(scope);
            var popover_class = element.data('popover-class');            

在 html 页面中,我已将 ID 作为角度表达式传递,如下所示

data-popover-template="{{field.popoverid}}" 

运行代码后,我收到Error: Syntax error, unrecognized expression: {{field.popoverid}} 之类的错误。

有人可以帮我解决这个问题吗?

【问题讨论】:

  • 试试这个data-popover-template="field.popoverid"
  • 非常感谢您的快速响应,通过此问题后,现在我如何在 ng-tempate 中传递相同的问题,如下所示

标签: jquery angularjs directive


【解决方案1】:

错误是不言自明的:data-popover-template 需要一个表达式。所以试试这个:

data-popover-template="field.popoverid"

表达式插值结果{{}}不再是表达式,因此这种情况下指令无法设置数据绑定。

【讨论】:

  • 非常感谢您的快速响应,通过此问题后,现在我如何在 ng-tempate 中传递相同的问题,如下所示
  • 这一行显示未定义的错误:var templateHtml = $('#' + templateId).html();
  • 我认为您不能拥有动态模板 ID。看起来您使用 popover AngularUI 指令错误。你能解释一下你想做什么吗?
  • 我正在尝试基于 JSON 数据响应创建动态弹出框,包括按钮和弹出框,因此我想分配弹出框 id 和目标 id 都形成 JSON 数据。
  • 用简单的<div id="..."> 代替脚本试试?
  • 猜你喜欢
    • 1970-01-01
    • 2017-03-10
    • 1970-01-01
    • 2013-11-25
    • 1970-01-01
    • 1970-01-01
    • 2019-07-04
    • 2018-06-13
    • 1970-01-01
    相关资源
    最近更新 更多