简体   繁体   English

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

[英]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-controlleronload

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.

相关问题 从AngularJS控制器,如何解析模块中定义的另一个控制器功能(动态ng-controller)? - From an AngularJS controller, how do I resolve another controller function defined in the module (dynamic ng-controller)? ng-controller变量在指令的链接函数中不可见 - ng-controller variables are not visible in directive's link function 具有范围和ng-controller的自定义angularjs指令 - custom angularjs directive with scope and ng-controller 使用ng-controller指令从AngularJS开始 - Starting with AngularJS using a ng-controller directive 使用模态ng-controller作为常规控制器 - Use a modal ng-controller as a regular controller AngularJS:ng-controller on指令不适用于指令中的transcluded元素 - AngularJS : ng-controller on directive does not work on transcluded elements within directive 如何设置通过ng-view指令或ng-controller指令创建的每个新作用域的侦听器? - How to set a listener to every new scope created though ng-view directive or ng-controller directive? 在AngularJS的指令中使用控制器名称(ng-controller)的表达式 - using expression for controller name(ng-controller) in a directive in AngularJS ng-controller指令和路由中的控制器有什么区别? - What is the difference between an ng-controller directive and a controller in the route? 如何修复angular js ng-controller函数调用? - How to fix angular js ng-controller function call?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM