【问题标题】:AngularJS Function within Function函数内的 AngularJS 函数
【发布时间】:2015-09-29 15:26:54
【问题描述】:

我的 AngularJS 应用程序中有一个通知下拉菜单。我想在打开下拉列表的函数中调用一个函数。这就是我的意思:

$scope.showNotif = false;

    $scope.toggleNotifDropdown = function(event) {
        $scope.showNotif = !$scope.showNotif;

        readNotifications = function() {
            NotificationService.readNotifs().then(
                function(success) {
                    console.log("Notifications read!");
                },
                function(errors) {
                    console.log("Something wrong happened.");
                }
            );
        };

        if($scope.showNotif) {
            $document.bind('click', $scope.globalNotifClose);
        } else {
            $document.unbind('click', $scope.globalNotifClose);
        }

        event.stopPropagation();
    };

通知下拉菜单完美运行,我只是无法让 readNotifications() 函数为我工作。任何建议都会很棒!谢谢!

【问题讨论】:

  • 你有没有调用过readNotifications()

标签: angularjs function methods callback


【解决方案1】:

在你的作用域函数中声明这个函数是没有意义的,你也永远不会调用它。外面声明,里面调用

$scope.toggleNotifDropdown = function (event) {
    $scope.showNotif = !$scope.showNotif;

    //  call the function declared below
    readNotifications();

    if ($scope.showNotif) {
        $document.bind('click', $scope.globalNotifClose);
    } else {
        $document.unbind('click', $scope.globalNotifClose);
    }

    event.stopPropagation();
};

// function declaration
var readNotifications = function () {
    NotificationService.readNotifs().then(

    function (success) {
        console.log("Notifications read!");
    },

    function (errors) {
        console.log("Something wrong happened.");
    });
};

【讨论】:

  • 是的!谢谢!我完全忘记了调用该函数。度过了一个奇怪的早晨。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-15
  • 2015-09-06
  • 1970-01-01
相关资源
最近更新 更多