Self invocation javascript function on AngularJS webpage

I have this self invoking function for an AngularJS webpage. As you can see, I have this getData () function, whose task is to make an ajax call to get some data when the page loads, and also in accordance with the interaction of users with the page.

<script type="text/javascript">
    // Should I defne it here like this completely outside the self invoking
    // function with or without var keyword
    getData = function (reqData) {
        alert(reqData); // Ajax call here...
    };

    (function () {

        // Should I defne it here like this? 
        getData = function (reqData) {
            alert(reqData); // Ajax call here...
        };

        //Should I have to use the var key word like so 
        var getData = function (reqData) {
            alert(reqData);// Ajax call here...
        };

        PatientCategoryController = function ($http, $scope, uiGridConstants) {

            // Should I defne it here like this inside the controller? 
            getData = function (reqData) {
                alert(reqData);// Ajax call here...
            };

            //Should I have to use the var key word inside the controller?
            var getData = function (reqData) {
                alert(reqData);// Ajax call here...
            };

            // Or should I define the function on the $scope object like so?
            $scope.getData = function (reqData) {
                alert(reqData);// Ajax call here...
            };

            angular.element(document).ready(getData('someDataToPass'));
        }
        PatientCategoryController.$inject = ['$http', '$scope', 'uiGridConstants'];
        angular.module('demoApp', ['ui.grid', 'ui.grid.autoResize', 'ui.grid.pagination']);
        angular.module('demoApp').controller('PatientCategoryController', PatientCategoryController);
    }());
</script>

My question is: how to define this function? Should this be defined on the $ scope object? Or do you need to determine on a par with the controller? Or should I define it completely outside the self invoking function?

Also in similar lines where I have to define a javascript object that will contain the data that may be required to call ajax.

, - javascript , . . javascript, , , javascript. Asp.Net mvc. javascript . , .

+4
2

- Angular, , .

- , Angular JS - ,

+3

, , , .

(function () {
    'use strict';
    angular
        .module('moduleName')
        .controller('controllerName', controllerName);

    controllerName.$inject = ['$rootScope', '$scope'];

    function controllerName($rootScope, $scope) {
        var vm = this;
        //define your variables here

        activate();//self invoking method

        function activate(){
            //do whatever you want to do here
        }
    }
})();

, .

+5

Source: https://habr.com/ru/post/1613341/


All Articles