重新加载并检查用户是否使用AngularJS连接到PhoneGap。

3
我正在使用PhoneGap创建一个应用程序。我已经成功检查了用户是否连接到互联网。但是,如果用户没有连接,我想放置一个按钮,以便用户可以点击重新加载并重新加载页面。以下是我的代码示例:
<ons-template id="directory.html">
    <ons-navigator var="app.navi" >
    <div ng-if="online"> <!-- Check If user is online or not -->
    <ons-page ng-controller="directoryControl">
      <ons-toolbar>
        <div class="left">
          <ons-toolbar-button ng-click="menu.toggle()">
            <ons-icon icon="ion-navicon" size="28px" fixed-width="false"></ons-icon>
          </ons-toolbar-button>
        </div>
        <div class="center">Directory List</div>
      </ons-toolbar>
     <p>Yes you are Connected!</p>
    </ons-page>
    </div>

    <div ng-if="!online">
      <ons-page>
      <ons-toolbar>
        <div class="left">
          <ons-toolbar-button ng-click="menu.toggle()">
            <ons-icon icon="ion-navicon" size="28px" fixed-width="false"></ons-icon>
          </ons-toolbar-button>
        </div>
        <div class="center">Directory List</div>
      </ons-toolbar>

        <p>Oops! You are not online..!<br/><ons-button ng-click="app.navi.pushPage('directory.html')">Reload</ons-button></p>
      </ons-page>
    </div>
  </ons-navigator>
</ons-template>

我希望用户点击这个按钮:<ons-button ng-click="app.navi.pushPage('directory.html')">重新加载</ons-button>,然后能够重新连接到他们之前离开的页面。
在这里,我使用了单页模板结构。
如果您想查看控制器,请参考下面的控制器代码,其中我完全没有使用ng-view/route。
    module.controller('directoryControl', function($scope, $http, $rootScope, ajaxCall) {
    ons.ready(function() {

var dataURL = "get_category_index";
var valuePickup = "categories"
ajaxCall.GetIndex($scope, dataURL, valuePickup);

$scope.setCurrentCategory = function(categoryName){
     $scope.CurrentCategory = categoryName;
     $rootScope.CurrentCategory=$scope.CurrentCategory;
            }
        });
    });

这里是否必须使用路由?还是有其他方法可以实现相同的效果?

我只想重新加载页面并停留在同一页上,而不需要重新启动整个过程。

1个回答

1
你可以使用Network cordova插件

https://github.com/apache/cordova-plugin-network-information

安装
cordova plugin add https://github.com/apache/cordova-plugin-network-information

示例代码

function checkConnection() {
    var networkState = navigator.connection.type;

    var states = {};
    states[Connection.UNKNOWN]  = 'Unknown connection';
    states[Connection.ETHERNET] = 'Ethernet connection';
    states[Connection.WIFI]     = 'WiFi connection';
    states[Connection.CELL_2G]  = 'Cell 2G connection';
    states[Connection.CELL_3G]  = 'Cell 3G connection';
    states[Connection.CELL_4G]  = 'Cell 4G connection';
    states[Connection.CELL]     = 'Cell generic connection';
    states[Connection.NONE]     = 'No network connection';

    alert('Connection type: ' + states[networkState]);
}

checkConnection();

这是我在AngularJS中已经拥有的。它运行良好。我只想在人们点击按钮后重新加载整个页面。 - user3201500

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接