$scope.$watch 带有 Angular 和 Ionic/Cordova 的 div 标签

$scope.$watch a div tag with Angular and Ionic/Cordova

我正在尝试在 Cordova 应用程序中使用 QRCode Read/scan。 QRCode QRCode 的响应在 qrcode.result 变量中。

我需要观看 div 并在它发生变化时进行处理。该过程包括导航到另一个 html。例如,如果 qrcode.result 等于句子 "ok"。 UI 必须更改为 "principal.html"。如果 qrcode.result 等于 "help",则应用程序必须更改为 "help.html"。你能帮帮我吗?

我的代码是:

<!--display result-->                
<div id="result" class="col" ng-model="result">{{result}}</div>

和控制器:

angular.module('starter.controllers', [])

.controller('DashCtrl', function($scope) {
    load();

    $scope.$watch('result', function(newVal, oldVal){
        alert(qrcode.result);
    }, true);  
});

我建议采用这种方法

<!--display result-->                
<div id="result" class="col">{{result}}</div>

您不需要ng-model,通常用于输入。

angular 方...

angular.module('starter.controllers', [])

.controller('DashCtrl', function($scope) {
    load();

    $scope.$watch(function() {
        return qrcode.result;
    }, function(newVal){
        $scope.result = newVal;
    }, true);  
});

您可以使用函数表达式观察 out-for-$scope 变量。