【发布时间】:2016-06-08 05:12:12
【问题描述】:
我想预填充一个 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");
});
};
});
【问题讨论】:
-
json.phoneNo 是否只包含一个电话号码。
标签: android angularjs cordova ionic-framework cordova-plugins