2014-02-25 234 views
1

我試圖創建自定義輸入指令,它將覆蓋複選框輸入的默認行爲。我似乎無法找到一個解決方案,防止默認複選框輸入指令執行。AngularJS自定義輸入指令。覆蓋默認輸入指令

我一直在努力,只是裝點輸入,定製指令的優先級設置爲0。

<input type="checkbox" my-custom-directive="0" ng-model="MyList" /> 

我還試圖用一個模板,更換不同的元素類型。

<div my-custom-directive="0" ng-model="MyList"></div> 
// part of directive object 
return { 
    restrict: 'A', 
    template: '<input type="checkbox" />', 
    replace: true, 
    terminal: true, 
    priority: 0, ... 
} 

最後我想要做的是一個複選框,輸入綁定到一個數組中,並創造行爲,如果一定值是數組的成員,輸入將被選中,否則它不會被檢查。另外,如果用戶檢查輸入框,指令會將提到的值添加到數組中。

回答

1

最後我用ngNonBindable指令解決了它。我真的不喜歡這個解決方案,因爲在更復雜的情況下,我可能仍然希望對模板的某些元素使用綁定。如果別人有更好的想法,一旦出現,我會接受這個答案。

return { 
    restrict: 'A', 
    template: '<input type="checkbox" ng-non-bindable />', 
    replace: true, 
    terminal: true, 
    priority: 0, ... 
}