AngularJS。如何从控制器组件外部调用控制器功能
如何在网页的任何位置(控制器组件之外)调用控制器下定义的函数?
当我按下“get”按钮时它完美地工作。但我需要从div控制器外部调用它。逻辑是:默认情况下我的div是隐藏的。在导航菜单的某处我按下一个按钮,它应该显示()我的div并执行“get”功能。我怎么能做到这一点?
我的网页是:
<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:
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){ } }); }; }
一只名叫tom的猫
扬帆大鱼
相关分类