【问题标题】:Filling input value from a controller function in angular.js - ionic framework从 angular.js 中的控制器函数填充输入值 - 离子框架
【发布时间】: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


【解决方案1】:

如果在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);  

【讨论】:

    猜你喜欢
    • 2018-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-26
    • 1970-01-01
    • 2016-08-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多