[英]How to use onload attribute with ng-controller directive to execute a function defined within the controller?
I have to call the submit()
function the moment the page loads. 页面加载时,我必须调用
submit()
函数。 However this sort of arrangement gives a submit()
method not found error. 但是这种安排给了一个
submit()
方法未找到的错误。 I am unable to understand the placement of ng-controller
and onload
. 我无法理解
ng-controller
和onload
。
Also if there is any alternative way of doing this with angular, please point that out as well. 另外,如果还有其他使用角度的方法,也请指出。
PS: This is a code snippet. PS:这是一个代码段。 All variables have been defined.
所有变量均已定义。
<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);
});
}
}]);
Use ng-init instead of onload ie 使用ng-init代替onload
<body ng-controller="DashboardDisplay" ng-init="submit()">
And removed scope which before arr in html ie {{scope.arr}} to {{arr}} 并删除了html中arr之前的范围,即{{scope.arr}}至{{arr}}
I have created working DEMO https://jsfiddle.net/Shital_D/x2k8n23n/1/ 我已经创建了可运行的演示https://jsfiddle.net/Shital_D/x2k8n23n/1/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.