簡體   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