我知道如何通过我的自定义指令传递指令,例如:通行证通过适用指令定制指令
page.html中
<my-directive read-only-attr="myVariable" label-style-attr="anotherVariable"></my-directive>
指令
myApp.directive("myDirective", function() {
return {
restrict: "E",
templateUrl: "myTemplate.html",
scope: {
readOnlyScopeVar: "=readOnlyAttr",
styleScopeVar: "=labelStyleAttr"
},
link: function (scope, element, attrs) {
}
};
});
模板
<div>
<label ng-style="styleScopeVar" />
<input type="text" ng-readonly="readOnlyScopeVar" />
</div>
我的模板比这个复杂得多,但我简化了它的问题。
我的问题是:如果用户没有在我的指令中指定“只读attr”或“label-style-attr”,我该如何防止ngReadonly和ngStyle运行?有大量的常见的角度指令,我想让人们适用于输入和模板内的其他元素(ngClass,ngDisabled,ngChange,ngPattern,ngIf等),但我不想全部运行它们,如果人没有在我的指令中指定他们。这就好像我需要一个模板来构建模板。
另外,请注意,我已阅读了有关transclusion的内容,但我不喜欢允许用户直接编辑输入元素的想法,并且在此示例中我可能想要应用诸如此类的多个元素如果只读attr引用为true,则可以更改标签颜色。