請記住,我很新的角度所以這可能不是最好的答案。
我想無論開關插件正在做的是防止複選框上的值被改變(或傳播到角度)。
我認爲你可以做的就是在你的指令上,將某些事情綁定到change事件上,並在指令中使用隔離範圍來與控制器中的變量進行雙向綁定。
HTML:
<div class="switch" init-switch switch-variable="testSwitch">
<input type="checkbox" id="check">
</div>
開關變量會做的指令
指令2路結合:
app.directive('initSwitch', function() {
return {
scope: {
switchVariable: '=' //this is how you define 2way binding with whatever is passed on the switch-variable attribute
},
link: function (scope, element, attr) {
scope.$evalAsync(function() {
element.switch();
});
element.on("change", function(){
if(scope.switchVariable)
scope.switchVariable = !scope.switchVariable
else
scope.switchVariable = true;
scope.$apply(); //I believe you need this to propagate the changes
});
}
}
});
編輯:
按照你的第一個發表評論,你可以嘗試改變指令事件:
element.on("change", function(){
if(element.children('.switch-off')[0]) //Looking for the class that determines to what side the switch is
scope.switchVariable = false;
else
scope.switchVariable = true;
scope.$apply(); //I believe you need this to propagate the changes
});
關於你的第二點,如果你設置爲選中的複選框,然後你的鏈接功能需要知道的複選框的狀態:
scope.$evalAsync(function() {
scope.switchVariable = element.children()[0].checked; //check if checkbox is checked (before the plugin changes the DOM
element.switch(); //initialize the plugin
});
你想要展示和隱藏什麼?因爲我唯一能看到的是帶有標籤和複選框的字段集。 – user3811714 2014-09-12 09:59:02
如果你正在談論contact/help中的複選框,它的工作正常。 – 2014-09-12 10:04:50
你的花式開關根本就沒有開關,請看這裏http://plnkr.co/edit/VaqEOLhJMVa4jvCmqKx8?p=preview – sylwester 2014-09-12 10:18:06