【问题标题】:$scope.$watch a div tag with Angular and Ionic/Cordova$scope.$watch 带有 Angular 和 Ionic/Cordova 的 div 标签
【发布时间】:2015-05-04 10:20:40
【问题描述】:

我正在尝试在 Cordova 应用程序中使用 QRCode 读取/扫描。 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);  
});

【问题讨论】:

标签: javascript angularjs cordova ionic


【解决方案1】:

我建议这种方法

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

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

对于角边...

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

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

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

您可以通过函数表达式观察 $scope 变量。

【讨论】:

  • 我更新了问题,以便在我获得二维码时更详细地了解流程
  • 您可以使用ng-include 指令或ngRoute 模块,具体取决于您的偏好。例如使用ng-include&lt;div ng-include="result === 'ok' ? 'principal.html' : 'help.html'"&gt;&lt;/div&gt;。 (顺便说一下有点乱的代码)
猜你喜欢
  • 2015-05-21
  • 2016-06-22
  • 2019-06-19
  • 1970-01-01
  • 2016-11-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多