如何动态添加Direcitves angularjs

How to Dynamically add Direcitves angularjs

本文关键字:添加 Direcitves angularjs 动态 何动态      更新时间:2024-07-17

我已经使用此代码成功地将angularjs指令添加到另一个指令中

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

但是如何动态地传递CCD_ 1n值。

$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 );

   }

是否可以传递该值或以任何其他方式传递该值?

如果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 );
}

如果对您有效,请告诉我们。