Javascript Angularjs$方法多次重复
让我们先看看代码,blow是我的按钮指令Javascript Angularjs$方法多次重复,javascript,angularjs,Javascript,Angularjs,让我们先看看代码,blow是我的按钮指令 .directive('ajaxButton', function() { return { restrict: 'A', scope:{ action: '&' }, link: function(scope, elem, attrs) { var formHtml = $(elem).html(); function buttonStatus(status) {
.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',函数($http){
返回{
保存数据:函数(数据){
返回$http.post('/wcsite',data);
},
getData:函数(){
返回$http.get('/wcsite/data');
}
};
});
正如您所看到的:当ajax请求成功时,$rootScope将$broadcast message:'httpend',但当我查看许多消息时,消息编号是add everytime…有谁能帮助我吗?非常感谢 您必须删除元素click处理程序之外的httpend:success处理程序