繁体   English   中英

如何在ng-controller指令中使用onload属性来执行控制器内定义的功能?

[英]How to use onload attribute with ng-controller directive to execute a function defined within the controller?

页面加载时,我必须调用submit()函数。 但是这种安排给了一个submit()方法未找到的错误。 我无法理解ng-controlleronload

另外,如果还有其他使用角度的方法,也请指出。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM