我想预填充一个input
字段,其值来自cordova插件(vliesaputra.deviceinformation
)。此插件基本上会自动返回用户的电话号码。尝试了很多,但价值没有预先填补。这是我的index.html和app.js文件
index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
<title></title>
<link href="lib/ionic/css/ionic.css" rel="stylesheet">
<link href="css/style.css" rel="stylesheet">
<!-- IF using Sass (run gulp sass first), then uncomment below and remove the CSS includes above
<link href="css/ionic.app.css" rel="stylesheet">
-->
<!-- ionic/angularjs js -->
<script src="lib/ionic/js/ionic.bundle.js"></script>
<!-- cordova script (this will be a 404 during development) -->
<script src="cordova.js"></script>
<!-- your app's js -->
<script src="js/loginApp.js"></script>
</head>
<body ng-app="login">
<ion-pane ng-controller="AutoFillPhoneNumber" data-ng-init="getPhoneNumber()" >
<ion-header-bar class="bar-stable">
<h1 class="title">Ionic Blank Starter</h1>
</ion-header-bar>
<ion-content>
<label class="item item-input">
<input type="text" ng-value="data.phoneNo">
</label>
</ion-content>
</ion-pane>
</body>
</html>
loginApp.js
var app = angular.module('login', ['ionic']);
app.run(function($ionicPlatform) {
$ionicPlatform.ready(function() {
// Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
// for form inputs)
if (window.cordova && window.cordova.plugins.Keyboard) {
cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
cordova.plugins.Keyboard.disableScroll(true);
}
if (window.StatusBar) {
// org.apache.cordova.statusbar required
StatusBar.styleDefault();
}
});
});
app.controller('AutoFillPhoneNumber',function ($scope){
$scope.data = {};
$scope.getPhoneNumber = function(){
var deviceInfo = cordova.require("cordova/plugin/DeviceInformation");
//alert(deviceInfo);
deviceInfo.get(function(result){
//alert(result);
var json = JSON.stringify(eval("(" + result + ")"));
var json = JSON.parse(json);
//alert(json);
alert(json.phoneNo);
$scope.data.phoneNo = json.phoneNo;
}, function(){
alert("failed");
});
};
});
答案 0 :(得分:0)
如果在 json.phoneNo 中,值即将到来,那么您需要尝试超时解决方案。
应用超时一些毫秒来设置$ scope.data.phoneNo中的值。
$timeout(function () {
$scope.data.phoneNo = json.phoneNo; // please check here $scope and json should be accessable, If not then make them accessable.
},500);