私はこの簡単なコードを持っています:
<modal ng-controler="Contacts">
//add new contact stuffs
</modal>
<div ng-controller="Contacts">
// show the new contact when added
</div>
.controller('Contacts', function ($scope, contactsFactory) {
contactsFactory.doSomething();
$scope.$on('contacts:addedNewContact', function (event, data) {
console.log('yo');
});
})
.factory('contactsFactory', function ($rootScope) {
var addNew = function (username, email) { //facoltative, we can get them via email OR username
return $http({
'method':'POST',
'url': appWS + '/api/contact',
'data': {
'email':email,
'username':username
}
}).then(function (response) {
if (response && response.status === 200) {
$rootScope.$broadcast('contacts:addedNewContact', response.data);
}
}).catch(function (err) {
$rootScope.$broadcast('contacts:errorAddingNewContact');
$window.console.error('Error while retrieving contact user data: ' + err);
});
};
return {
'addNew':addNew
};
});
ブロードキャストの連絡先:addedNewContact は、実際にはコンソールに「yo」を 2 回入力しています。
なぜ1回だけではなく2回発射されるのか理解できません。
どんな助けでも感謝します、ありがとう