2015-09-29 60 views
0

我的AngularJS应用程序中有一个通知下拉列表。我想在打开下拉列表的函数中调用一个函数。这里是我的意思是:函数内的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()为我工作。任何建议都会很棒!谢谢!

+2

你是否曾经调用'readNotifications()'? – ryanyuyu

回答

0

在你的作用域函数中声明函数并且你永远不会调用它是没有意义的。在外部声明并从内部调用它

$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."); 
    }); 
}; 
+0

是的!谢谢!我完全忘记了调用这个函数。有一个奇怪的早晨。谢谢! – user3794832