让我们先看看代码,吹是我的Button指令
.directive('ajaxButton', function() {
return {
restrict: 'A',
scope:{
action: '&'
},
link: function(scope, elem, attrs) {
var formHtml = $(elem).html();
function buttonStatus(status) {
var loadText = status == 'start' ? '提交...' : formHtml;
var isDisabled = status == 'start' ? true : false;
$(elem).attr('disabled',isDisabled).html(loadText);
}
elem.click(function(){
buttonStatus('start');
//-------call function -------------
scope.$apply(function () {
scope.action();
});
//-------listen events-----------------
scope.$on('httpend:success',function() {
$.ambiance({
message: "success!",
title: "message",
type: "success"
});
console.log('success!!!');
buttonStatus('end');
});
});
}
};
});
这是我的工厂服务和控制器:
homepage.controller('homeCtrl',function ($scope,homeservice,$rootScope) {
homeservice
.getData()
.success(function (data) {
$scope.model = JSON.parse(data.homepage);
$rootScope.$broadcast('httpend');
});
$scope.model = {
homeTitle: 'this is home title',
keyword: 'this is keyword',
messageCover: 'http://ww1.sinaimg.cn/bmiddle/6538fd8cjw1eb0jzna64mj209605kaa7.jpg',
logoImg: 'http://ww1.sinaimg.cn/bmiddle/6538fd8cjw1eb0jzna64mj209605kaa7.jpg',
homgBg: 'http://ww1.sinaimg.cn/bmiddle/6538fd8cjw1eb0jzna64mj209605kaa7.jpg'
};
$scope.updateData = function () {
homeservice.
saveData({data: JSON.stringify($scope.model) })
.success(function (data) {
if(data) {
$rootScope.$broadcast('httpend:success');
}
});
};
}) .factory('homeservice',function($ http){ 返回{ saveData:function(data){ return $ http.post('/ wcsite',data); }, getData:function(){ return $ http.get('/ wcsite / data'); } }; });
正如你所看到的:当ajax请求获得成功时,$ rootScope将广播消息:'httpend',但是当我重新启动许多消息时,每次都会添加消息号....任何可以帮助我的人?非常感谢!!
答案 0 :(得分:1)
您必须在元素单击处理程序之外删除httpend:success处理程序。