Javascript 角度切换开关如何检测更改/单击?

Javascript 角度切换开关如何检测更改/单击?,javascript,angularjs,Javascript,Angularjs,我是一个尝试使用CGARVIS设计的角度切换开关的角度新手:。不幸的是,没有太多的文档,像ng click等“正常”角度选项无法工作 我已经根据JSON对象中是否存在某个值来设置切换按钮的初始值: <div ng-repeat="agendapunt in aktieveVergadering.Agendapunten | orderBy:'VolgNr'" ng-init="search(agendapunt.AgendapuntCollegelid)">

我是一个尝试使用CGARVIS设计的角度切换开关的角度新手:。不幸的是,没有太多的文档,像ng click等“正常”角度选项无法工作

我已经根据JSON对象中是否存在某个值来设置切换按钮的初始值:

    <div ng-repeat="agendapunt in aktieveVergadering.Agendapunten | orderBy:'VolgNr'" ng-init="search(agendapunt.AgendapuntCollegelid)">
        <ul class="list-group">
            <li class="list-group-item">

                <table class="table-condensed apunttable">
                    <tr>
                        <td class="apuntvolgnr"><span class="badge pull-left">{{agendapunt.VolgNr}}</span></td>
                        <td class="apuntomschrijving">{{agendapunt.Omschrijving}}</td>
                        <td class="apuntkeuze"><toggle-switch model="Bespreken" on-label="Ja" off-label="Nee"  /></td>
                    </tr>
                </table>
            </li>
        </ul>
    </div>

  • {{agendapunt.VolgNr} {{agendapunt.Omschrijving}
初始真/假的设置由该功能完成:

$scope.search = function (array) {
    this.Bespreken = false;
    for (var i = 0; i < array.length; i++) {
        if (array[i].Volledigenaam == $scope.VolledigeNaam)
            this.Bespreken = true;
            return;
    }
    return;
};
$scope.search=函数(数组){
这个.Bespreken=false;
对于(var i=0;i
现在,这给了我一个包含listitems的页面,每个页面都有一个设置为正确初始值的togglebutton。但是:

-如何检测每个单独按钮的更改(或单击事件)

-为什么我不能像$scope.Bespreken那样以角度方式引用“Bespreken”值

  • 在哪里可以找到有关使用角度切换开关的更多信息/示例

您可以使用watchCollection自动检测模型中的更改

$scope.$watchCollection('switchStatus', function() { ... });

尝试在切换开关中添加更改,如下所示:

<toggle-switch
  model="switchStatus"
  on-label="Hide"
  off-label="Show"
  on-change="switchFilters()">
<toggle-switch>
但在此之前,请将其添加到模块中

var app= angular.module('app', ['toggle-switch']);
您正在使用

<toggle-switch model="Bespreken"... ></toggle-switch>

应该是

<toggle-switch ng-model="Bespreken"... ></toggle-switch>

我使用ng单击此方式解决了范围中的一些问题:

<toggle-switch 
  ng-model="Bespreken" 
  ng-click="switch(Bespreken)" 
  on-label="Ja"
  off-label="Nee">
</toggle-switch>


希望它能对任何可能遇到这种情况的人有所帮助,我最后在指令范围中添加了on change,这样我就可以使用唤醒师答案中的语法了。在angular-toggle-switch.js文件中只需要几行代码,它似乎工作正常免责声明:我对angular有些陌生,所以这可能不是理想的方法。

scope: {
    disabled: '@',
    onLabel: '@',
    offLabel: '@',
    knobLabel: '@',
    change: '&onChange'   //Allows us to bind to a function call
},
...
...
...
link: function(scope, element, attrs, ngModelCtrl) {
    ...
    ...
    ...
    scope.toggle = function toggle() {
        if (isEnabled) {
            scope.model = !scope.model;
            ngModelCtrl.$setViewValue(scope.model);
            scope.change();   //After updating the value, execute the callback
        }
    }
}

请为JSFIDLE提供代码。不起作用,您需要ng模型来进行ng更改,即使您这样做了,也会忽略它……您在哪里看到ng更改?这是变化,它为我工作!它是指令中的内置选项@德文?你能回答吗?你正在使用其他版本的角度切换开关。正式版本-这里的哪一点-没有关于更改的内容…ng click和ng change对我有效,但是关于更改的另一个解决方案没有。是的,这一点都不清楚。您不应该更改库文件:)只是好奇,根据GitHub页面,它在MIT许可下,如果我没有弄错的话,允许修改。我明白你的意思,做很多更改,特别是对较大的第三方文件,有时可能会导致意外的后果,但当许可协议允许时,我不明白为什么会有这样的问题。
scope: {
    disabled: '@',
    onLabel: '@',
    offLabel: '@',
    knobLabel: '@',
    change: '&onChange'   //Allows us to bind to a function call
},
...
...
...
link: function(scope, element, attrs, ngModelCtrl) {
    ...
    ...
    ...
    scope.toggle = function toggle() {
        if (isEnabled) {
            scope.model = !scope.model;
            ngModelCtrl.$setViewValue(scope.model);
            scope.change();   //After updating the value, execute the callback
        }
    }
}