我有一个简单的设置,我想单击一个按钮并从输入控件获取数据。问题是 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);
};
}]);
关于angularjs - phonegap/ionic - 单击功能内的 $scope 未更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24027497/