Angularjs ng show不适用于复选框开关
我正在使用一个基于引导开关的复选框开关(此处的文档:) 我想在元素上使用ng show,就像这里的示例中一样: 但是,当我单击开关时,什么也没有发生(选中绑定到开关的复选框) 我使用现有的plunker将问题置于正确的背景下: HTML:Angularjs ng show不适用于复选框开关,angularjs,Angularjs,我正在使用一个基于引导开关的复选框开关(此处的文档:) 我想在元素上使用ng show,就像这里的示例中一样: 但是,当我单击开关时,什么也没有发生(选中绑定到开关的复选框) 我使用现有的plunker将问题置于正确的背景下: HTML: 测试开关 测试开关 ... 谢谢 您必须将代码嵌套在ng应用程序中,以便它知道这是一个有角度的应用程序 <div ng-app="myApp"> <fieldset> <le
测试开关
测试开关
...
谢谢 您必须将代码嵌套在ng应用程序中,以便它知道这是一个有角度的应用程序
<div ng-app="myApp">
<fieldset>
<legend>Test switch</legend>
<div class="form-group" >
<label for="testSwitch">Test switch</label><br>
<div class="switch">
<input type="checkbox" ng-model="testSwitch"/>
</div>
</div>
</fieldset>
<h1 ng-show="testSwitch">Now you see me</h1>
</div>
测试开关
测试开关
现在你看到我了
您必须将代码嵌套在ng应用程序中,以便它知道这是一个有角度的应用程序
<div ng-app="myApp">
<fieldset>
<legend>Test switch</legend>
<div class="form-group" >
<label for="testSwitch">Test switch</label><br>
<div class="switch">
<input type="checkbox" ng-model="testSwitch"/>
</div>
</div>
</fieldset>
<h1 ng-show="testSwitch">Now you see me</h1>
</div>
测试开关
测试开关
现在你看到我了
您必须将代码嵌套在ng应用程序中,以便它知道这是一个有角度的应用程序
<div ng-app="myApp">
<fieldset>
<legend>Test switch</legend>
<div class="form-group" >
<label for="testSwitch">Test switch</label><br>
<div class="switch">
<input type="checkbox" ng-model="testSwitch"/>
</div>
</div>
</fieldset>
<h1 ng-show="testSwitch">Now you see me</h1>
</div>
测试开关
测试开关
现在你看到我了
您必须将代码嵌套在ng应用程序中,以便它知道这是一个有角度的应用程序
<div ng-app="myApp">
<fieldset>
<legend>Test switch</legend>
<div class="form-group" >
<label for="testSwitch">Test switch</label><br>
<div class="switch">
<input type="checkbox" ng-model="testSwitch"/>
</div>
</div>
</fieldset>
<h1 ng-show="testSwitch">Now you see me</h1>
</div>
测试开关
测试开关
现在你看到我了
请记住,我对Angular很陌生,所以这可能不是最好的答案 我认为Switch插件所做的一切都是为了防止复选框上的值被更改(或传播到angular) 我认为您可以做的是在指令上,将某些内容绑定到更改事件,并在指令上使用隔离作用域与来自控制器的变量进行双向绑定 HTML:
编辑: 根据您的第一条评论,您是否可以尝试将指令事件更改为:
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
});
请记住,我是个新手,所以这可能不是最好的答案 我认为Switch插件所做的一切都是为了防止复选框上的值被更改(或传播到angular) 我认为您可以做的是在指令上,将某些内容绑定到更改事件,并在指令上使用隔离作用域与来自控制器的变量进行双向绑定 HTML:
编辑: 根据您的第一条评论,您是否可以尝试将指令事件更改为:
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
});
请记住,我是个新手,所以这可能不是最好的答案 我认为Switch插件所做的一切都是为了防止复选框上的值被更改(或传播到angular) 我认为您可以做的是在指令上,将某些内容绑定到更改事件,并在指令上使用隔离作用域与来自控制器的变量进行双向绑定 HTML:
编辑: 根据您的第一条评论,您是否可以尝试将指令事件更改为:
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
});
请记住,我是个新手,所以这可能不是最好的答案 我认为Switch插件所做的一切都是为了防止复选框上的值被更改(或传播到angular) 我认为您可以做的是在指令上,将某些内容绑定到更改事件,并在指令上使用隔离作用域与来自控制器的变量进行双向绑定 HTML:
编辑: 根据您的第一条评论,您是否可以尝试将指令事件更改为:
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
});
你到底想展示和隐藏什么?因为我唯一能看到的是一个带有标签和复选框的字段集。如果你在contact/help中谈论复选框,它可以正常工作。你的奇特开关根本没有切换,请看这里@sss是右开关-切换是通过jQuery完成的,在角度上下文之外,所以它不会工作。你可以:1。摆脱这个花哨的开关2。破解js库并添加作用域。$apply在适当的位置3。查找其他具有角度的UI工具包friendly@MiTa-第二个选项是我唯一的选择,因为我在这个项目中被迫使用fancy switch和特定的UI工具包。我真的很感激任何建议的解决方案。你到底想展示和隐藏什么?因为我唯一能看到的是一个带有标签和复选框的字段集。如果你在contact/help中谈论复选框,它可以正常工作。你的奇特开关根本没有切换,请看这里@sss是右开关-切换是通过jQuery完成的,在角度上下文之外,所以它不会工作。你可以:1。摆脱这个花哨的开关2。破解js库并添加作用域。$apply在适当的位置3。查找其他具有角度的UI工具包friendly@MiTa-第二个选项是我唯一的选择,因为我在这个项目中被迫使用fancy switch和特定的UI工具包。我真的很感激任何建议的解决方案。你到底想展示和隐藏什么?因为我唯一能看到的是一个带有标签和复选框的字段集。如果您在contact/help中谈论复选框,它工作正常。您的奇特开关根本没有切换,请看这里@sss是正确的-切换是通过