我知道如何通過我的自定義指令傳遞指令,例如:通行證通過適用指令定製指令
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,則可以更改標籤顏色。