简体   繁体   English

脚本加载后如何定义Angularjs控制器?

[英]How do I define an Angularjs controller after a script has loaded?

I am trying to "angularize" the code in this quickstart guide on how to use the google calendar api. 我正在尝试将快速入门指南中的代码“角度化”,以了解如何使用Google Calendar API。

I have the following code. 我有以下代码。 For now, I am just trying to have a page that says true if the user needs to be logged into google, and false if they already have. 现在,我只是想有一个页面,说true ,如果用户需要登录到谷歌和false如果他们已经有了。

<html ng-app="calApp">
<head>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.7/angular.min.js"></script>
    <script src="https://apis.google.com/js/client.js"></script>
    <script type="text/javascript">

        var app = angular.module("calApp",[]);

        app.controller('calController',function calController($scope){
            gapi.auth.authorize({
                'client_id': 'asdfghjkl123456',
                'scope': 'https://www.googleapis.com/auth/calendar.readonly',
                'immediate': true
            }, function(authResult){
                if (authResult && !authResult.error) {
                    $scope.needslogin = false;

                }else{
                    $scope.needslogin = true;
                }
            });
        });

    </script>
</head>
<body ng-controller="calController">
        {{needslogin}}
</body>
</html>

The problem is that the gapi.auth.authorize part gives me an error because it attempts to run before client.js has loaded. 问题是gapi.auth.authorize部分给我一个错误,因为它尝试在加载client.js之前运行。

The expected way to solve this is using a callback function. 解决此问题的预期方法是使用回调函数。 So I attempted 所以我尝试

<script src="https://apis.google.com/js/client.js?onload=defineController"></script>
<script type="text/javascript">

    var app = angular.module("calApp",[]);

    function defineController(){
        app.controller('calController',function calController($scope){
            gapi.auth.authorize({
                'client_id': 'asdfghjkl123456',
                'scope': 'https://www.googleapis.com/auth/calendar.readonly',
                'immediate': true
            }, function(authResult){
                if (authResult && !authResult.error) {
                    $scope.needslogin = false;

                }else{
                    $scope.needslogin = true;
                }
            });
        });
    }
</script>

but now I get an error because the controller is not defined when <body ng-controller="calController"> attempts to run. 但是现在出现错误,因为在<body ng-controller="calController">尝试运行时<body ng-controller="calController">

Any tips on how to properly do this would be appreciated. 有关如何正确执行此操作的任何提示将不胜感激。

You can't define your controller after bootstrap 引导后无法定义控制器

try 尝试

<script src="https://apis.google.com/js/client.js?onload=gapiBootCallback"></script>
<script type="text/javascript">

    var gapiBootStrapper = {}; // an object that you can attach a callback function to in the controller

    var app = angular.module("calApp", []).constant('gapiBootStrapper', gapiBootStrapper); // Passing it into Angular as a constant is not necessary but stop us using global from within a controller

    function gapiBootCallback() {
        gapi.auth.authorize({
            'client_id': 'asdfghjkl123456',
            'scope': 'https://www.googleapis.com/auth/calendar.readonly',
            'immediate': true
        }, function (authResult) {
            if (authResult && !authResult.error) {
                gapiBootStrapper.callback(false);
            } else {
                gapiBootStrapper.callback(true);
            }
        });
    }

    app.controller('calController', function calController($scope, $timeout, gapiBootStrapper) {
        gapiBootStrapper.callback = function (needslogin) {
            $timeout(function () { // Use $timeout so we don't need to call $scope.$apply
                $scope.needslogin = needslogin;
            });
        };
    });
</script>

You can try to manually bootstrap your app in the call back 您可以尝试在回叫中手动引导您的应用

<html>

<head>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.7/angular.min.js"></script>
    <script type="text/javascript">

        var app = angular.module("calApp",[]);

        app.controller('calController',function calController($scope){
            gapi.auth.authorize({
                'client_id': 'asdfghjkl123456',
                'scope': 'https://www.googleapis.com/auth/calendar.readonly',
                'immediate': true
            }, function(authResult){
                if (authResult && !authResult.error) {
                    $scope.needslogin = false;

                }else{
                    $scope.needslogin = true;
                }
            });
        });
        var callback = function() {

           angular.bootstrap(document, ['calApp']);
        };           
    </script>
    <script src="https://apis.google.com/js/client.js?onload=callback">  </script>

</head>

<body ng-controller="calController">{{needslogin}}
</body>

</html>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在AngularJS中通过AJAX加载数据后,如何重新初始化数据表? - How do I re-initialize datatables AFTER data has loaded via AJAX in AngularJS? 我如何告诉Selenium Angular控制器已“加载” - How do I tell Selenium that an Angular controller has “loaded” 如何在加载后使用JavaScript调整Google地图的大小? - How do I resize a Google Map with JavaScript after it has loaded? 在将控制器加载到CodeIgniter中后运行Java脚本 - Run an java script after controller has been loaded in CodeIgniter 内部脚本完全加载后如何运行脚本? - How do I run a script after internal scripts are fully loaded? 从外部控制器加载控制器后,AngularJS添加回调 - Angularjs adding callback after controller has been loaded from outside controller AngularJS:如何初始化嵌入了ajax加载模板的控制器? - AngularJS: How can I initialize controller embedded with ajax loaded template? 加载视图并渲染AngularJS,UI路由器后,在控制器中触发一些代码 - Trigger some code in controller after view has been loaded, and rendered AngularJS, ui-router 在AngularJS中加载部分视图后,如何触发DOM的操纵? - How do I trigger the manipulation of the DOM after a partial view is loaded in AngularJS? 将图像加载到AngularJS ngview中后,如何执行Foundation js调用? - How do I execute foundation js call after images are loaded in AngularJS ngview?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM