2016-12-03 41 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 –