angularjs - phonegap/ionic - 单击功能内的 $scope 未更新

标签 angularjs cordova ionic-framework

我有一个简单的设置,我想单击一个按钮并从输入控件获取数据。问题是 click 函数内部的 $scope 变量保持与代码首次运行时相同的值。当我在网络上创建 fiddle 时,一切都完全按照计划进行。

HTML

<div ng-app>
    <div ng-controller="TodoCtrl">
        <input type="text" ng-model="authenticationCode">
        <br>
        <button class="button" ng-click="authenticate()">Authenticate</button>
        <br>
        <span>Auth info: {{authinfo}}</span>
        <br>
        <span>authenticationCode - {{authenticationCode}}</span>
    </div>
</div>

JavaScript

function TodoCtrl($scope) {
    $scope.authenticationCode = 'TESTER';
$scope.authenticate = function(){
        var t = $scope.authenticationCode;
    $scope.authinfo = t;
    };
}

Fiddle of the code working in web

当我在 PhoneGap 中运行它时

  • 屏幕加载时输入框中显示“TESTER”
  • 屏幕加载时将“TESTER”作为“authenticationCode -”的值
  • 将输入中的“TESTER”更改为“CHANGED”会将“authenticationCode -”更新为“CHANGED”
  • 点击“身份验证”会将值“TESTER”设置为“身份验证信息:”

我不知道为什么 $scope.authenticationCode 没有更新它在身份验证函数中的值。我也不确定为什么这在网络上有效,并且我在使用 Ionic 的 PhoneGap 中遇到了问题。

最佳答案

出现此问题的原因可能是 TodoCtrl 中使用了原语,请尝试此操作 -

HTML

<div ng-controller="TodoCtrl">
    <label class="item item-input">
        <input type="text" ng-model="input.authenticationCode">
    </label>
    <br>
    <button class="button" ng-click="authenticate()">Authenticate</button>
    <br><br>
    <span>Auth info: {{input.authInfo}}</span>
    <br>
    <span>authenticationCode - {{input.authenticationCode}}</span>
</div>

JavaScript

.controller('TodoCtrl', ['$scope', function($scope) {
    $scope.input = {
        authenticationCode: 'TESTER',
        authInfo: ''
    };

    $scope.authenticate = function() {
        $scope.input.authInfo = $scope.input.authenticationCode;
        console.log('authInfo: ' + $scope.input.authInfo);
    };
}]);

Here is a CodePen of the above

关于angularjs - phonegap/ionic - 单击功能内的 $scope 未更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24027497/

相关文章:

javascript - 如何保护 HTML5+PhoneGap 移动应用程序中的数据?

javascript - angularjs防止ng-option自动选择最后一个值

javascript - Cordova - 如何修改 webview 标志?

ios - 为什么我的 Phonegap 应用程序覆盖了我移动设备的状态栏?

android - ionic cordova android build error (cordova.cmd build android exited with exit code 1)

reactjs - 通过 React useState() 处理表单数据的更好方法?

javascript - 在 AngularJS 中 Controller 和工厂之间共享 $scope

angularjs - 动态添加 ng-bind 指令不起作用吗?

javascript - Angularjs $anchorScroll.yOffset 不适用于 ng-sticky header

http - http请求调用方法的难点