加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

在AngularJS网页上自行调用javascript函数

发布时间:2020-12-17 17:50:11 所属栏目:安全 来源:网络整理
导读:我有一个AngularJS网页的自调用功能.正如您所看到的,我有这个getData()函数,其作用是进行ajax调用以在页面加载时获取一些数据,以及随后根据用户与页面的交互. script type="text/javascript" // Should I defne it here like this completely outside the se
我有一个AngularJS网页的自调用功能.正如您所看到的,我有这个getData()函数,其作用是进行ajax调用以在页面加载时获取一些数据,以及随后根据用户与页面的交互.

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

我的问题是如何定义这个功能?应该在$scope对象上定义吗?或者应该与控制器相提并论?或者我应该在自调用函数之外完全定义它?

同样在类似的行中我应该定义javascript对象,它将保存ajax调用可能需要的数据.

我开始构建这个页面,在某些时候,javascript开始表现得很糟糕,所以我不得不从头开始.这就是我提出这个问题的原因.
在过去,我已阅读并尝试了解javascript,但除了在浏览器方面,我还没有做过任何严肃的javascript编程.这个应用程序本身在Asp.Net mvc.所以我对javascript一直没有信心并且不断陷入困境.请指导我.

解决方法

这样的东西应该作为Angular服务创建,然后在需要时注入你的应用程序和控制器.

如果你想让它成为全球范围内可用的东西,这个问题Angular JS – Make service globally accessible from controllers and view应该给你一些很好的指导.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读