如何将参数传递给将影响模板angularjs选择的指令

How to pass parameters to a directive that will influence the choice of a template angularjs

本文关键字:angularjs 选择 指令 影响 参数传递      更新时间:2023-09-26

我有一个指令,它可以使用3个或4个模板中的一个,具体取决于从上一页传递的参数。如果在选择在指令中使用哪个模板时无法访问范围变量,如何将信息传递给指令。我首先想到的是使用我动态写入模板的指令标记的属性,但我猜这是行不通的。

你猜对了。你应该使用attributes.通过'='双向绑定将详细信息传递给你的指令。你应该将模板变量传递给该attribute。你可以在你的指令中获得该属性的值。当值由于双向绑定而自动改变时

例如,我有一个名为我的指令组件的指令

内部html

内部指令

angular.module('directive',[]).directive('myDiretive', function(){
    return {
         restrict: 'E',
         scope: { template: '='  },
         template: '<p>ur template</p>',
         link: function (scope, element, attrs) {
}
};
});