Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
AngularJS控制器初始化_Angularjs - Fatal编程技术网

AngularJS控制器初始化

AngularJS控制器初始化,angularjs,Angularjs,Angular JS Newbie:我得到的gapi没有定义,因为sydney.userAuthed在加载API之前被调用。但是我不明白为什么在创建控制器时调用方法sydney.auth。我在加载页面时出错,没有用户交互 calling auth api.js:22 ReferenceError: gapi is not defined at Object.sydney.userAuthed (http://localhost:8888/js/api.js:36:8) at Ob

Angular JS Newbie:我得到的
gapi没有定义
,因为
sydney.userAuthed在加载API之前被调用。但是我不明白为什么在创建控制器时调用方法
sydney.auth
。我在加载页面时出错,没有用户交互

calling auth api.js:22
ReferenceError: gapi is not defined
    at Object.sydney.userAuthed (http://localhost:8888/js/api.js:36:8)
    at Object.sydney.auth (http://localhost:8888/js/api.js:23:30)
    at new LoginController (http://localhost:8888/js/controllers.js:8:24)
    at invoke (http://localhost:8888/lib/angular/angular.js:2902:28)
    at Object.instantiate (http://localhost:8888/lib/angular/angular.js:2914:23)
    at http://localhost:8888/lib/angular/angular.js:4805:24
    at updateView (http://localhost:8888/lib/angular/angular-ui-router.js:931:30)
    at <error: illegal access>
    at Object.Scope.$broadcast (http://localhost:8888/lib/angular/angular.js:8307:28)
    at $state.transition.resolved.then.$state.transition (http://localhost:8888/lib/angular/angular-ui-router.js:747:20) angular.js:5754
OAuth2 API loaded api.js:13
控制器定义为:

function LoginController($scope, $state) {
  $scope.auth = sydney.auth($state);
}
sydney.auth
方法用于使用
GoogleAPI客户端库for JavaScript
对用户进行身份验证

var sydney = sydney || {};

sydney.auth = function($state) {
    console.log("calling auth");
    sydney.signin(false, sydney.userAuthed($state));
}

sydney.signin = function(mode, callback) {
      gapi.auth.authorize({client_id: sydney.CLIENT_ID,
        scope: sydney.SCOPES, immediate: mode,
        response_type: 'token id_token'},
        callback);
      console.log("signin called");
}

sydney.userAuthed = function($state) { 
      var request =
          gapi.client.oauth2.userinfo.get().execute(function(resp) {
        if (!resp.code) {
          var token = gapi.auth.getToken();
          token.access_token = token.id_token;
          gapi.auth.setToken(token);
          // User is signed in, call my Endpoint
          gapi.client.realestate.owner.create().execute(function(resp) {
              alert("Signed In");
              $state.transitionTo("signedin");
            });
        }
      });
    }
编辑

正确答案是将
$scope.auth
定义为一个函数:

function LoginController($scope, $state) {
  $scope.auth = function() { 
      sydney.auth($state);
  }
}

似乎您要做的是使
sydney.auth
函数在您的范围内可用。如果是这样,您应该将您的
LoginController
更改为:

function LoginController($scope, $state) {
  $scope.auth = sydney.auth;
}

您的代码所做的是调用
sydney.auth
函数并将
$scope.auth
设置为其返回值,我认为这不是您想要的。

您对此有什么建议吗?您可以尝试将gapi添加到函数变量中,比如这个函数($state,gapi)答案被接受,因为它帮助我找到了正确的解决方案。用正确答案编辑的问题。
function LoginController($scope, $state) {
  $scope.auth = sydney.auth;
}