在同一页面上使用ngstomp的多个angularjs控制器

时间:2017-06-17 19:57:20

标签: angularjs stomp

我正在使用ngStomp将websocket订阅连接到angularjs控制器。当我在页面上有一个控制器时,它工作正常。当我在之前的div中添加另一个控制器时,它会停止工作。

这是因为它试图将stomp连接两次到同一个端点吗?

我的控制器声明是:

var market = 'all';
var orderApp = angular.module('openOrderApp', ['ngStomp']); 
orderApp.controller('openOrderController', function ($stomp, $scope) {
    $stomp.connect('https://localhost/marketdata-websocket', {})
        .then(function (frame) {
            console.log('Subscribing to /topic/openOrders');
            var subscription = $stomp.subscribe('/topic/openOrders', 
                function (payload, headers, res) {
                        $scope.exchanges = payload;
                        $scope.$apply($scope.orders);
                });
            $stomp.send("/app/openOrders", market);
     });
});

var market = 'all';
var statusApp = angular.module('statusApp', ['ngStomp']); 
statusApp.controller('statusController', function ($stomp, $scope) {
    $stomp.connect('https://localhost/marketdata-websocket', {})
        .then(function (frame) {
            console.log('Subscribing to /topic/exchangeStatus');
            var subscription = $stomp.subscribe('/topic/exchangeStatus', 
                function (payload, headers, res) {
                        $scope.exchanges = payload;
                        $scope.$apply($scope.exchanges);
                });
            $stomp.send("/app/exchangeStatus", market);
     });
});

在一个div中有:

<div class="openOrders row" ng-app="openOrderApp" ng-controller="openOrderController">

接下来是:

<div class="exchangeStatus row" ng-app="statusApp" ng-controller="statusController">

当我删除打开的订单div时,状态控制器正常工作。

1 个答案:

答案 0 :(得分:0)

在阅读了angularjs文档后,而不是继续使用它:P

不能在页面上声明两个ng-app。显然可以有多个控制器。显然有很多方法可以构建它,但我最终将两个控制器应用于模块。

var app = angular.module('app', ['ngStomp']); 
var controllers = {};
controllers.OpenOrderController = function ($stomp, $scope) {
    console.log("Creating order controller");
    etc...
};
controllers.ExchangeController = function ($stomp, $scope) {
    console.log("Creating exchange controller");
    etc...
};
app.controller(controllers);

然后在html标签

<html ng-app>

div中的控制器:

<div ng-controller="OpenOrderController">

<div ng-controller="ExchangeController">