3

使用此代碼如何動態地添加Direcitves angularjs

var newElement = $compile("<div my-diretive='n'></div>")($scope); 
$element.parent().append(newElement); 

我已經成功地增加了一個angularjs指令到另一個指令,但我怎麼能動態傳遞my-diretive='n' N值。

$scope.showDirective = function(item){ 

    var newElement = $compile("<div my-diretive='n'></div>")($scope); 
    //here i want to replace 'n' with item 

    $element.parent().append(newElement); 


    } 

是否可以通過該值或任何其他方式來做到這一點?

回答

1

如果myDirective具有隔離範圍,這將創造雙向數據綁定:

$scope.showDirective = function(item){ 

    $scope.item = item; 
    // the interpolation will be against $scope with item available on it 
    var newElement = $compile("<div my-diretive='item'></div>")($scope); 
    $element.parent().append(newElement); 
} 

讓我們知道,如果你的作品。

+0

感謝它工作:) – nish