Làm cách nào tôi có thể gọi hàm được xác định trong bộ điều khiển từ bất kỳ vị trí nào của trang web (bên ngoài thành phần của bộ điều khiển)?
Nó hoạt động hoàn hảo khi tôi nhấn nút "get". Nhưng tôi cần gọi nó từ bên ngoài bộ điều khiển div. Logic là: theo mặc định div của tôi bị ẩn. Ở đâu đó trong menu điều hướng, tôi nhấn một nút và nó sẽ hiển thị () div của tôi và thực hiện chức năng "get". Làm thế nào tôi có thể đạt được điều này?
Trang web của tôi là:
<div ng-controller="MyController">
<input type="text" ng-model="data.firstname" required>
<input type='text' ng-model="data.lastname" required>
<form ng-submit="update()"><input type="submit" value="update"></form>
<form ng-submit="get()"><input type="submit" value="get"></form>
</div>
Js của tôi:
function MyController($scope) {
// default data and structure
$scope.data = {
"firstname" : "Nicolas",
"lastname" : "Cage"
};
$scope.get = function() {
$.ajax({
url: "/php/get_data.php?",
type: "POST",
timeout: 10000, // 10 seconds for getting result, otherwise error.
error:function() { alert("Temporary error. Please try again...");},
complete: function(){ $.unblockUI();},
beforeSend: function(){ $.blockUI()},
success: function(data){
json_answer = eval('(' + data + ')');
if (json_answer){
$scope.$apply(function () {
$scope.data = json_answer;
});
}
}
});
};
$scope.update = function() {
$.ajax({
url: "/php/update_data.php?",
type: "POST",
data: $scope.data,
timeout: 10000, // 10 seconds for getting result, otherwise error.
error:function() { alert("Temporary error. Please try again...");},
complete: function(){ $.unblockUI();},
beforeSend: function(){ $.blockUI()},
success: function(data){ }
});
};
}
get()
MyContoder từ bộ điều khiển khác?