AngularJS 函数中的函数
AngularJS Function within Function
我的 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() 为我工作。任何建议都会很棒!谢谢!
在你的作用域函数中声明函数是没有意义的,你也永远不会调用它。在外面声明,在里面调用
$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.");
});
};
我的 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() 为我工作。任何建议都会很棒!谢谢!
在你的作用域函数中声明函数是没有意义的,你也永远不会调用它。在外面声明,在里面调用
$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.");
});
};