在服务内部使用时,ngToast返回未定义

时间:2019-07-14 11:24:00

标签: javascript ngtoast angularjs-1.7

我正在尝试通过本文档http://tamerayd.in/ngToast/使用ngToast创建一个简单的通知系统。当在我的模块(登录流程)中使用它时,一切似乎都能正常工作,但是如果我试图将其传递给服务($ notify),它将在控制台中返回未定义。事实是,我第一次将此服务用作测试时,它可以工作。一旦为我的需求定制了它,问题就出现了。请注意,我对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)
        }
    });

1 个答案:

答案 0 :(得分:0)

Nvm-确实很小。我实际上是将“ isError”作为参数发送给通知系统,而不是为其分配值。很明显,我真的很想问问题。