我已经使用这段代码成功地将一个 angularjs 指令添加到另一个指令中
var newElement = $compile( "<div my-diretive='n'></div>" )( $scope );
$element.parent().append( newElement );
但是我如何动态传递 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 );
}
是否可以传递值或任何其他方式?
最佳答案
如果 myDirective
有隔离范围,这将创建双向数据绑定(bind):
$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 );
}
如果它适合您,请告诉我们。
关于javascript - 如何动态添加指令angularjs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20540723/