【问题标题】:'pusher-angular' not works in ionic application“推角”在离子应用中不起作用
【发布时间】:2017-02-17 09:38:55
【问题描述】:

我正在使用离子框架和推送器实时技术开发推送器应用程序。当我在 android 应用程序中运行时,app.js 中的 'pusher-angular' 依赖注入在 ionic 应用程序中不起作用。任何建议如何解决它?我想在控制器中启用 $pusher 依赖注入,以便我可以在我的应用程序中使用 Pusher 实时 API。有什么建议吗?

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>


    <!-- compiled css output -->
    <link href="css/ionic.app.css" rel="stylesheet">
    <link href="css/style.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="lib/ngCordova/dist/ng-cordova.js"></script>
    <script src="cordova.js"></script>

    <!-- your app's js -->
  <script src="//js.pusher.com/3.2/pusher.min.js"></script>
    <script src="//cdn.jsdelivr.net/angular.pusher/latest/pusher-angular.min.js"></script>
    <script src="js/app.js"></script>
    <script src="js/controllers.js"></script>
    <script src="js/services.js"></script>
  </head>
  <body ng-app="starter" >
    <ion-nav-view></ion-nav-view>
  </body>
</html>

app.js

    angular.module('starter', ['ionic', 'starter.controllers', 'starter.services','ngCordova'])

    .run(function($ionicPlatform, $cordovaSQLite) {
      $ionicPlatform.ready(function() {
        if (window.cordova && window.cordova.plugins && window.cordova.plugins.Keyboard) {
          cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
          cordova.plugins.Keyboard.disableScroll(true);

        }
        if (window.StatusBar) {
          StatusBar.styleDefault();
        }

        db = window.openDatabase("chatChannel.db", "1", "Demo SQLite Test", "2000");
        $cordovaSQLite.execute(db, "CREATE TABLE IF NOT EXISTS chat_channel(id interger primary key, chat_room text, last_text text, username text, chat_channel text unique)")
      });
    })

Controller.js

    .controller('ChatRoomCtrl', ['$scope', '$state', '$rootScope',         '$ionicScrollDelegate',
    function($scope, $state, $rootScope,$ionicScrollDelegate){

      var client = new Pusher('ed05a79be9c11b452872', {
          cluster: 'ap1',
          encrypted: true
        });
      var my_channel = client.subscribe(subscribeChannel);
      my_channel.bind('chat_message', function(data) {
          console.log(data);
          $scope.messages.push(data);
      });

    }

【问题讨论】:

  • 控制台有错误吗?您可以将代码添加到您的帖子中(通过编辑它)吗?尤其是在 index.html 中添加 pusher 脚本的地方,以及使用它的控制器。
  • @e666 我已经通过编辑包含了我的代码。
  • 我看不到你在 angularjs 中注入 pusher-angular 的位置。另外,你在控制台有什么错误吗?

标签: android angularjs cordova ionic-framework pusher


【解决方案1】:

我看到的第一个问题是index.html

<script src="//js.pusher.com/3.2/pusher.min.js"></script>
<script src="//cdn.jsdelivr.net/angular.pusher/latest/pusher-angular.min.js"></script>

此行在 Android 应用程序中不起作用,但在浏览器中起作用。这是因为android文件是从file://加载的,所以它会尝试加载file://js.pusher.com/3.2/pusher.min.js

您将需要像这样精确协议:

<script src="https://js.pusher.com/3.2/pusher.min.js"></script>
<script src="https://cdn.jsdelivr.net/angular.pusher/latest/pusher-angular.min.js"></script>

【讨论】:

  • 是的,这就是问题所在。谢谢你真的帮助我。
猜你喜欢
  • 1970-01-01
  • 2021-03-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-25
  • 2021-02-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多