Javascript 用联系人填充列表

Javascript 用联系人填充列表,javascript,android,cordova,ionic-framework,Javascript,Android,Cordova,Ionic Framework,我正在创建一个离子安卓应用程序,使用插件读取联系人,并将其显示在离子列表中 index.html app.js controllers.js 我的输出:- 该应用程序启动良好,点击添加新联系人按钮后,一段时间后,我会收到一个提示,显示联系人数量。但离子列表中只有一项显示,即“Mohit” 但是,当我再次单击“添加新联系人”按钮时,所有联系人都会填充到离子列表中 我的问题是:- 正如我所解释的,我认为该应用程序只需点击一次就可以正常工作。知道是什么引起的吗 我对爱奥尼亚框架和javascript都

我正在创建一个离子安卓应用程序,使用插件读取联系人,并将其显示在离子列表中

index.html

app.js

controllers.js

我的输出:-

该应用程序启动良好,点击添加新联系人按钮后,一段时间后,我会收到一个提示,显示联系人数量。但离子列表中只有一项显示,即“Mohit”

但是,当我再次单击“添加新联系人”按钮时,所有联系人都会填充到离子列表中

我的问题是:-

正如我所解释的,我认为该应用程序只需点击一次就可以正常工作。知道是什么引起的吗

我对爱奥尼亚框架和javascript都是新手,所以如果这是一个简单的错误,请告诉我


谢谢。

答案很简单,我只需在更改了。 在这种情况下,更新$scope.CListItems之后

<!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/app.js"></script>
    <script src="js/controllers.js"></script>
  </head>
  <body ng-app="starter" ng-controller="CListControl">

    <ion-pane>
      <ion-header-bar class="bar-royal">
        <h1 class="title">Contact Reader</h1>
        <button class="button" ng-click="showcontact()">Add New Contact</button>
      </ion-header-bar>
      <ion-content>
      <h3 id="Data">Welcome to my app created by Ionic Cortova !! Yeh !!! Hello</h3>

        <ion-list >
            <ion-item ng-repeat="item in CListItems">
            {{item.displayName}}
            </ion-item>
        </ion-list>

      </ion-content>

    </ion-pane>
    <script>
    </script>
  </body>
</html>
// Ionic Starter App

// angular.module is a global place for creating, registering and retrieving Angular modules
// 'starter' is the name of this angular module example (also set in a <body> attribute in index.html)
// the 2nd parameter is an array of 'requires'
angular.module('starter', ['ionic','starter.controllers'])

.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);
    }
    if(window.StatusBar) {
      StatusBar.styleDefault();
    }
  });
})
angular.module('starter.controllers', [])
    .controller('CListControl', function ($scope) {

        $scope.CListItems=[{displayName: 'Mohit'}];
        document.getElementById("Data").innerHTML="Undone";

        //funtion to add items to the existing list
        $scope.showcontact = function() {
            console.log("before event");
            document.getElementById("Data").innerHTML="Done";
        document.addEventListener("deviceready", init, false);

        function init() {
            console.log("init");
            var options      = new ContactFindOptions();
        options.multiple= true;
        options.hasPhoneNumber = true;
        var fields       = [navigator.contacts.fieldType.displayName,navigator.contacts.fieldType.phoneNumbers];
        navigator.contacts.find(fields, gotContacts, errorHandler, options);
        console.log("init end");
        }


        //Error Alerting
        function errorHandler(e) {
        alert("errorHandler: "+e);
        }

        //Script to remove all the contacts without phonenumber.

        function gotContacts(c) {

        var i=0;
        len=c.length;
        while (i<len)
        {
            if (c[i].phoneNumbers==null)
            {
                c.splice(i,1);
                if (i!=0)   {i-=1;}
                len-=1;
            }
            else
            {
                $scope.CListItems.push(c[i]);
            }
            i+=1;

        }
        alert("Found:"+$scope.CListItems.length+" Contacts");

        }

        }
});