<button id="voterListSearchButton" class="serachButton" ng-click="onSearchButton1Click()">search</button>
這是我的搜索按鈕:Angularjs單擊發射2次?
$scope.onSearchButton1Click = function() {
var partId = $("#partIdSearch").val();
var firstName = $("#firstNameSearch").val();
var lastName = $("#lastNameSearch").val();
var fathersName = $("#fathersNameSearch").val();
var address = $("#address").val();
$rootScope.$broadcast('onSearchButton1Click',partId,firstName,fathersName,lastName,address);
}
這是我對上面的按鈕控制器。我廣播這個事件到另一個控制器通過以下方式:
$scope.$on('onSearchButton1Click',function(event,data1,data2,data3,data4,data5){
pId= data1;
fname=data2;
lname=data3;
fathersName=data4;
address= data5;
VoterDataService.search(data1,data2,data3,data4,data5,0).success(function(response){
$scope.arr = response.search;
});
});
這是我的服務代碼:
voterDataServiceAPI.search = function(data1,data2,data3,data4,data5,pageNo,lang) {
var url ="api/search" + "?part_id="+data1 +"&first_name="+data2+"&middle_name="+data3+"&last_name="+data4+"&subpart=" + data5 +"&page_no="+pageNo;
return $http({
method : "GET",
url:url,
headers:{
"content-type": "application/json"
}
});
}
爲什麼單次點擊火災2倍?
我想你的控制器調用兩次!!! – 2014-09-11 05:50:23
如果您已經創建了一項服務,然後爲什麼要進行廣播,而不是直接從控制器調用服務方法 – Seminda 2014-09-11 05:50:57
您的控制器調用了兩次。如何將控制器綁定到頁面。使用ng-controller或路由?或者兩者都 – Seminda 2014-09-11 05:52:50