2015-10-09 21 views
2

我在使用我正在開發的MeanJS應用程序中的控制器中使用angular-websocket時遇到了一些困難。'Unknown Provider'試圖在MeanJS v0.4.1應用中包含angular-websocket

我的應用程序基於MeanJS v0.4.1。

bower install angular-websocket --save  

這創建的目錄/公共/ LIB /角的WebSocket

接着我添加它來/config/assets/default.js

lib: { 
     css: [ 
     ... 
     ], 
     js: [ 
     ... 
     'public/lib/angular-websocket/angular-websocket.js' 
     ], 
     tests: ['public/lib/angular-mocks/angular-mocks.js'] 
    }, 

予先用安裝了它

在我的/modules/core/client/app/config.js文件中,我添加了它作爲依賴項:

var applicationModuleVendorDependencies = [ 
     ... 
     'angular-websocket' 
    ]; 

最後,在我的角度模塊本身,

angular.module('somemodule').controller('ModulenameController', ['$scope', '$http', '$stateParams', '$location', 'Authentication', 'SomeModule', 'ngWebSocket', 
     function ($scope, $http, $stateParams, $location, Authentication, SomeModule, ngWebSocket) { 

當我去查看我的網頁,我可以在Chrome的開發工具,它包括作爲源的「源」選項卡上看到,

我想在我的控制器使用這個文件,是這樣的:

var dataStream = ngWebSocket('wss://www.somesite.com/realtime'); 

    dataStream.onMessage(function(message) { 
     console.log("dataStream Message: " + message); 
     $scope.orderBook = message; 
    }); 

    dataStream.send('{"op":"subscribe", "args":"someargument"}'); 

然而,我的控制檯顯示以下錯誤:

Error: [$injector:unpr] Unknown provider: ngWebSocketProvider <- ngWebSocket <- ModuleNameController 

一些我已經試過的東西:

  1. 改變基準名稱
    按照文檔(https://www.npmjs.com/package/angular-websocket

    angular.module('YOUR_APP', [ 
        'ngWebSocket' // you may also use 'angular-websocket' if you prefer 
    ]) 
    

我用「試圖ngWebSocket ','angular-websocket',但我仍然遇到同樣的問題。

  • 我試圖希望通過我的代碼,看看,也許這正被重新定義爲angularJS文檔瀏覽:

    https://docs.angularjs.org/error/ $噴油器/ unpr

  • 指出導致此錯誤的原因可能是'使用angular.module API重新定義模塊'。

    任何幫助將不勝感激。

    回答

    2

    工廠實際上是命名爲$websocket,所以你應該做如下:

    angular.module('somemodule').controller('ModulenameController', ['$scope', '$http', '$stateParams', '$location', 'Authentication', 'SomeModule', '$websocket', 
        function ($scope, $http, $stateParams, $location, Authentication, SomeModule, $websocket) { 
    
    +0

    謝謝!這工作!我會在8分鐘內接受你的回答 –

    +0

    沒問題!樂意效勞 :) – taxicala

    相關問題