简体   繁体   English

$ scope。在Angular中的$ watch在Ionic / Cordova中不起作用

[英]$scope.$watch in Angular doesn't work in Ionic/Cordova

I'm building an app using the Ionic Framework for which I now want to create some on-off switches (which are basically fancy styled checkboxes). 我正在使用Ionic Framework构建一个应用程序,我现在想要创建一些开关(基本上是花哨的样式复选框)。 When a person flips the switch I want to send this to the server, so I'm first trying to catch the switch change event. 当一个人翻转开关我想把它发送到服务器,所以我首先尝试捕捉开关更改事件。 In a basic angular install I tried it with a simple checkbox in the html ( <input type="checkbox" ng-model="theswitch"> ) and a $watch in my controller like so: 在基本的角度安装中,我尝试使用html中的简单复选框( <input type="checkbox" ng-model="theswitch"> )和我的控制器中的$watch ,如下所示:

$scope.$watch('theswitch', function(){
    console.log('theswitch changed');
    if ($scope.boel){
        console.log('theswitch is TRUE');
    } else {
        console.log('theswitch IS FALSE');
    }
});

This works like a charm. 这就像一个魅力。 I now implemented a similar thing in my Ionic app. 我现在在我的Ionic应用程序中实现了类似的功能。 My HTML is like this: 我的HTML是这样的:

<ion-item class="item-toggle">
    Mail
    <label class="toggle">
        <input type="checkbox" ng-model="mail">
        <div class="track">
            <div class="handle"></div>
        </div>
    </label>
</ion-item>

and my controller looks like this: 我的控制器看起来像这样:

myControllers.controller('AccountCtrl', function ($scope) {
    $scope.$watch('mail', function(){
        console.log('THE MODEL CHANGED');
        if ($scope.mail) {
            console.log('The button was turned ON.');
        } else {
            console.log('The button was turned OFF.');
        }
    });
});

Upon loading the screen I get two logs: 加载屏幕后,我得到两个日志:

THE MODEL CHANGED
The button was turned OFF.

After that I can flip the switch a million times, but I get no log messages anymore, not even an error. 之后,我可以将开关翻转一百万次,但我不再收到任何日志消息,甚至没有错误。 Since it works perfectly in my angular test website, but not in my app, I'm kinda lost in what could be wrong. 因为它在我的角度测试网站上完美运行,但不在我的应用程序中,我有点迷失在可能出错的地方。

Would anybody have any idea what I can do to catch this switch event? 有人知道我能做些什么才能抓住这个切换事件吗? All tips are welcome! 欢迎所有提示!

Possible reason for not working $scope.$watch might be property overriding in child scope. 不工作$scope.$watch可能原因$scope.$watch可能是覆盖子范围的属性。 If you set $watch on controller scope, but checkbox with ng-model binding is created inside some child scope of the controller, child scope might create its own mail property and controller $scope.$watch will not be able to detect changes in that property. 如果在控制器范围上设置$watch ,但是在控制器的某个子范围内创建了带有ng-model绑定的复选框,子范围可能会创建自己的mail属性和控制器$ scope。$ watch将无法检测到该更改属性。 Here is jsfiddle that demonstrates this problem: link . 这是jsfiddle,它演示了这个问题: 链接

To avoid this problem you should not use primitive values on the scope for ng-model bindings. 要避免此问题,不应在ng-model绑定的范围上使用原始值。 Instead you should bind to properties of objects on the scope, for example ng-model="formData.mail" where formData is some object in the controller's scope. 相反,您应该绑定到作用域上对象的属性,例如ng-model="formData.mail" ,其中formData是控制器范围内的某个对象。 To watch for object property changes you also use dot syntax: $scope.$watch('formData.mail', function(){...}) 要查看对象属性更改,还可以使用点语法: $scope.$watch('formData.mail', function(){...})

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM