2016-12-03 20 views
0

我正在使用Nunjucks渲染模板加班时点击正确的URL。这是一块模板:在angularJS脚本中使用模板变量?

<h3>Select some text </h3> 
<div ng-app="myApp"> 
    {% for i in result %} 
<div id={{i._id}} class="task" commentId = {{i._id}} get-popover-content> {{i.text}} </div> <br/> 
{% endfor %} 

我也把一个角JS脚本在此模板,现在,我只是想弄清楚什么是传递变量的最好办法{{我._id}}要在指令内使用(可能,我想这i._id发送到我的数据库来获取信息

<script> 
var app = angular.module("myApp", []); 
app.directive("getPopoverContent", function($http) { 
    return { 
    link: function(scope, element, attr) { 
     element.popover(); 
     $(element).on('mouseover', function(e){ 
     console.log('i._id = ',{{i._id}}); 
    }) 
    }) 

} }});

  1. 这是使用模板引擎+ angularJS的正确方法吗?
  2. 有没有办法做到这一点?

回答

0

有两种方法可以做到这一点,你可以将它绑定到指令范围。

var app = angular.module("myApp", []); 
 
app.directive('getPopoverContent', function() { 
 

 
    function link(scope, element, attrs) { 
 
    console.log(scope.commentId); 
 
    } 
 

 
    return { 
 
    link: link, 
 
    restrict: 'A', 
 
    scope: { 
 
     commentId: '=commentId' 
 
    } 
 
    }; 
 
});

或者你可以只使用ATTRS参数把它弄出来的任何属性

var app = angular.module("myApp", []); 
 
    app.directive('getPopoverContent', function() { 
 

 
     function link(scope, element, attrs) { 
 
     console.log(attrs.commentId); 
 
     } 
 

 
     return { 
 
     link: link, 
 
     } 
 
     }; 
 
    });

+0

我已经试过像这样的: VAR app = angular.module(“myApp”,[]); ();'mouseover',function(e){ console();();(mouseover),function(e){ console.datctive(“getPopoverContent”,function(){ function link(scope,element,attrs){ .LOG(scope.commentId); 的console.log(attrs.commentId); }) } 返回{ 链接:链接, 限制: 'A', 范围:{ commentId: '= commentId' } } }); 只是为了检查,但它没有奏效。我得到的是commentId是未定义的...任何线索@dangh –