[英]From an AngularJS controller, how do I resolve another controller function defined in the module (dynamic ng-controller)?
[英]How to use onload attribute with ng-controller directive to execute a function defined within the controller?
頁面加載時,我必須調用submit()
函數。 但是這種安排給了一個submit()
方法未找到的錯誤。 我無法理解ng-controller
和onload
。
另外,如果還有其他使用角度的方法,也請指出。
PS:這是一個代碼段。 所有變量均已定義。
<body ng-controller="DashboardDisplay" onload="submit()">
<div class="container-fluid" >
{{scope.arr}}
</div>
</body>
<script>
var myApp = angular.module('myApp',[]);
myApp.controller('DashboardDisplay', ['$scope','$http',function($scope,$http) {
$scope.submit = function(){
var jsonOb = {"A":"B"};
$http.post(URL,jsonOb).
success(function(response) {
console.log('got it' + response);
$scope.arr=response;
}).
error(function(data, status, headers, config) {
console.log('nufin' + status);
});
}
}]);
使用ng-init代替onload
<body ng-controller="DashboardDisplay" ng-init="submit()">
並刪除了html中arr之前的范圍,即{{scope.arr}}至{{arr}}
我已經創建了可運行的演示https://jsfiddle.net/Shital_D/x2k8n23n/1/
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.