Мне просто интересно, как я могу проверить событие handleAddClientBroadcast
?Как проверить, что событие транслировалось в AngularJS?
У меня есть навигационное обслуживание как так:
angular.module("ruleManagement.services")
.factory('navigationService', function ($rootScope) {
var navigationService = {};
navigationService.prepForBroadcast = function() {
this.broadCastIsAddClientItem();
};
navigationService.broadCastIsAddClientItem = function() {
$rootScope.$broadcast('handleAddClientBroadcast');
};
return navigationService;
});
Я впрыснуть эту услугу навигации в мой clientsCtrl
и поймать handleAddClientBroadcast
как так:
$scope.$on('handleAddClientBroadcast', function() {
$scope.clientModel = {
id: 0,
name: "",
description: "",
rules: []
};
var lastClient = _.findLast($scope.clients);
if (typeof lastClient == 'undefined' || lastClient == null) {
lastClient = $scope.clientModel;
}
$scope.clientModel.id = lastClient.id + 1;
$scope.clients.push($scope.clientModel);
});
Спасибо.
Я нашел этот полезный тоже: http://stackoverflow.com/questions/15272414/how-can-i-test-events-in-angular – Ben
возможный дубликат из [Как проверить $ scope. $ on в AngularJS] (http://stackoverflow.com/questions/19441782/how-do-i-test-scope-on-in-angularjs) – ivarni