简体   繁体   English

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

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

I am trying to use Angular with SignalR in my demo application. 我正在尝试在演示应用程序中将Angular与SignalR一起使用。 I am trying to use $q service to use promises. 我正在尝试使用$ q服务来使用Promise。 Don't know whats wrong in my code but its not working. 不知道我的代码有什么问题,但无法正常工作。

SERVICE 服务

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 CONTROLLER

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

its not working 它不起作用

Just wrap it in a $timeout . 只需将其包装在$timeout This will perform a safe $apply if necessary. 如有必要,这将执行安全的$apply

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM