簡體   English   中英

Angular Promise($ q)無法正常工作

[英]Angular Promise ($q) is not working

我正在嘗試在演示應用程序中將Angular與SignalR一起使用。 我正在嘗試使用$ q服務來使用Promise。 不知道我的代碼有什么問題,但無法正常工作。

服務

var boardConsole = $.connection.builtinboard;
var chat = angular.module('chat', []);
chat.factory('board', ['$q', '$timeout', function ($q, $timeout) {
var board = {};
board.startBoard = function (callback) {
    $.connection.hub.start(function () {
        if (angular.isFunction(callback)) {
            callback();
        }
    });
};
board.loadAllMessages = function () {
    var deferred = $q.defer();
    boardConsole.server.loadAllMessages().done(function (messages) {
        deferred.resolve(messages);
    }).fail(function () {
        deferred.reject(function () {
            //SHOW NOTHING FOUND 
        });
    });
    return deferred.promise;
};
return board;
} ]);

CONTROLLER

chat.controller('chatController', ['$scope', 'board', function ($scope, board) {
$scope.Messages = [];
board.startBoard(function () {
    board.loadAllMessages().then(function (messages) {
        alert('1');
        $scope.Messages = messages;
    });
});
} ]);

它不起作用

只需將其包裝在$timeout 如有必要,這將執行安全的$apply

$timeout(function(){
    deferred.resolve(messages);
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM