1

我正在尝试通过本文档 - http://tamerayd.in/ngToast/使用 ngToast 创建一个简单的通知系统。在我的模块(登录流)中使用它时,一切似乎都正常工作,但如果我试图将它传递给服务($notify),它会在控制台中返回 undefined。问题是我第一次使用这项服务作为测试,它奏效了。一旦我根据自己的需要对其进行了定制,问题就出现了。请注意,我对 angularjs 不是很好,而且我在做这个项目时正在学习它。如果您发现任何奇怪的东西或不是“最佳实践”,请告诉我(这部分可能有点离题,所以如果您愿意,请忽略它)。

我已经尝试删除任何感觉多余的代码并更改参数顺序,但结果是相同的。请注意,当使用 ngToast 作为“登录流”控制器内的依赖项时,这有效,但只要我将其传递给“$notify”服务,问题就会开始出现。

应用模块:index-globals.js

const licentaApp = angular.module("licenta-app", ["ngToast"]);
licentaApp.run(($rootScope, $location) => {
    $rootScope.$on('$locationChangeSuccess', function() {
        return $location.$$hash;
    });
}).controller("app", ($scope, $rootScope, $location) => {
    $scope.hash = "";
    $rootScope.$on('$locationChangeSuccess', function() {
        $scope.hash = $location.$$hash;
    });
});

index-login-controller.js - 调用 $notify 服务的控制器

licentaApp.controller("login-flow", ($scope, $http, $notify,ngToast) => {
    // $scope.username = "admin@admin.com";
    // $scope.password = "pass";
    $scope.isLoading = false;
    $scope.login = () => {
        $scope.isLoading = true;
        $scope.loading_class = "spinner-border text-light";
        $scope.login_text = "";
        $http({
            url: "attempt-login",
            method: "GET",
            params: {
                username: $scope.email,
                password: $scope.password
            },
            headers: {
                "Content-Type": "text/plain"
            }
        }).then(function(result) {
            console.log(ngToast)
            if (result.data !== "no_result") {
                $notify.displayNotification("Bine ați venit!",ngToast);
                location.hash = "profil";
            } else {
                $notify.displayNotification("Datele de logare nu au fost introduse corect.", ngToast,isError);
            }
            $scope.isLoading = false;
        }).catch(err => {
            $notify.displayNotification("A aparut o eroare: " + err);
            $scope.isLoading = false;
        });
    }
    $scope.forgotPass = () => {
        location.hash = "uitat_parola";
    }
});

licentaApp.controller("forgot-pass-flow", ($scope, $rootScope, $location, $http, $timeout, $notify) => {
    function verifyCNP(cnp) {
        let sum = 0;
        controlNumber = "279146358279";
        if (cnp.length !== 13) {
            $notify.displayNotification($rootScope, $timeout, "Unul dintre codurile numerice este incorect.", true);
            return false;
        } else {
            for (let i = 0; i < 12; i++) {
                sum += parseInt(cnp[i]) * parseInt(controlNumber[i]);
            }
            let controlDigit = sum % 11 === 10 ? 1 : sum % 11;
            if (controlDigit !== parseInt(cnp[12])) {
                $notify.displayNotification($rootScope, $timeout, "Unul dintre codurile numerice este incorect.", true);
                return false;
            } else {
                return true;
            }
        }
    }
    $scope.isLoading = false;
    $scope.back = () => {
        location.hash = "";
    }

    $scope.reset = () => {
        if ($scope.cnp && $scope.cnp_repeat) {
            if (verifyCNP($scope.cnp) === false || verifyCNP($scope.cnp_repeat) === false) {
                return;
            } else if (!$scope.email || !$scope.email.match(/([A-Z0-9!#$%&'*+-/=?^_`{|}~.])/gi)) {
                $notify.displayNotification($rootScope, $timeout, "Email-ul este incorect.", true);
            } else {
                alert("TEST")
            }
        } else {
            $notify.displayNotification($rootScope, $timeout, "Unul dintre câmpurile de CNP nu a fost completat.", true);
        }
    }
});

$notify.js - 通知服务


licentaApp
    .service("$notify", function () {
        this.displayNotification = (message, ngToast,isError) => {
             ngToast.create({
                 className : isError ? "danger" : "success",
                 content : message,
                 dismissOnClick : true,
                 animation : "slide",
                timeout : 5000
             });
            // console.log(ngToast)
        }
    });
4

1 回答 1

1

Nvm - 这是非常小的事情。我实际上是将“isError”作为参数发送到通知系统,而不是为其分配一个值。这感觉很明显,我实际上对首先提出问题感到难过。

于 2019-07-14T11:33:32.477 回答