简体   繁体   English

AngularJS Toaster 在回调中不显示

[英]AngularJS Toaster does not show on callback

I'm using AngularJS Toaster to show toast messages.我正在使用 AngularJS Toaster 来显示 Toast 消息。 But i can't seem to get it working with a result from a callback, and i REALLY don't know why.但是我似乎无法使用回调的结果让它工作,我真的不知道为什么。

I've created a service for Toaster:我为烤面包机创建了一个服务:

service.factory('$toasterservice', ['toaster',
    function (toaster) {
        return {
            'show': function (type, title, text) {
                return toaster.pop({
                    type: type,
                    title: title,
                    body: text,
                    showCloseButton: false
                });
            }
        };
    }]);

This piece of code is in my controller:这段代码在我的控制器中:

$scope.submit = function (db) {

            var params = {
                username: db.credentials.username,
                password: db.credentials.password,
                database: db.name,
                hostname: db.host,
                port: db.port
            };

            $dbservice.testConnection(params, function (response) {
                if (response.error)
                {
                    console.log(response.error)
                }
                else
                {
                    console.log('correct!');                    
                }
            });

        };

whenever i put this line of code:每当我把这行代码:

$toasterservice.show('error', 'TITLE', 'BODY');

within controller level scope, it works perfectly fine.在控制器级别范围内,它工作得很好。

When ever i try to use it in:当我尝试在以下情况下使用它时:

$dbservice.testConnection(params, function (response) {
             //HERE $toasterservice.show('error', 'TITLE', 'BODY');
            });

it doesn't work, how can i solve this?它不起作用,我该如何解决这个问题? i can't seem to understand why this is happening.我似乎无法理解为什么会这样。

$dbservice : $dbservice :

service.factory('$dbservice', ['$constants',
function ($constants) {

    return {
        'testConnection': function (params, callback) {               

            $.ajax({
                url: $constants.URL_TEST_CONNECTION,
                dataType: 'json',
                type: 'post',
                contentType: 'application/x-www-form-urlencoded',
                data: params,
                success: callback,
                error: callback
            });
        }
    };
}]);

The problem is using $.ajax and you should switch to using $http .问题是使用$.ajax ,您应该切换到使用$http

Any events that occur outside of angular core that change the scope need to notify angular so it can run digest in view.在 angular 核心之外发生的任何改变范围的事件都需要通知 angular,以便它可以在视图中运行摘要。

You can call $scope.$apply() in the callback of your $.ajax to run digest您可以在$.ajax的回调中调用$scope.$apply()来运行摘要

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

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