Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/423.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
Javascript 跟踪Google文档后,无法从回调函数访问$scope_Javascript_Angularjs_Google Cloud Endpoints - Fatal编程技术网

Javascript 跟踪Google文档后,无法从回调函数访问$scope

Javascript 跟踪Google文档后,无法从回调函数访问$scope,javascript,angularjs,google-cloud-endpoints,Javascript,Angularjs,Google Cloud Endpoints,我已经按照(向下滚动)的步骤使用云端点引导AngularJS。我的代码如下所示: index.html <!DOCTYPE html> <html ng-app="AnimalsApp"> <head> <meta charset="UTF-8"> <title>Animal Manager Server</title> <script src="https://ajax.googleapis.com/ajax/lib

我已经按照(向下滚动)的步骤使用云端点引导AngularJS。我的代码如下所示:

index.html

<!DOCTYPE html>
<html ng-app="AnimalsApp">
<head>
<meta charset="UTF-8">
<title>Animal Manager Server</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.28/angular.min.js"></script>
<script src="js/animal.js"></script>
<script src="https://apis.google.com/js/client.js?onload=init"></script>
</head>
<body>
  <div ng-controller="MainCtrl" class="container">
    {{message}} world
  </div>
</body>
</html>
文本
成功了显示在控制台日志中,表明已执行回调。但是,设置
$scope.is\u backend\u ready=true对显示
没有影响。这使我相信回调中的
$scope
对象工作不正常


我做错了什么?谷歌文档有错吗?

您使用
$scope.$apply()
的意图是正确的,但它位于错误的位置。您需要在回调中执行,而不是在回调中执行。按照您现有的方式,您正在执行函数
load\u guestbook\u lib
,然后执行摘要循环。由于
gapi.client.load
函数是异步的,因此它会在以后运行,并且不会在上下文之外发生摘要

尝试:

$window.init=function(){
$scope.load_guestbook_lib();//只需调用函数
};
$scope.load_留言簿_lib=function(){
load('creatureCloud','v1',function(){
$scope.is_backend_ready=true;
$scope.message='Hello cats';
console.log('make it!');

$scope.$apply();//您使用
$scope.$apply()的意图
是正确的,但它位于错误的位置。您需要在回调中执行,而不是在回调之外。按照您现有的方式,您正在执行函数
load\u guestbook\u lib
,然后执行摘要循环。由于
gapi.client.load
函数是异步的,因此它会在以后运行,并且不会在上下文之外发生摘要

尝试:

$window.init=function(){
$scope.load_guestbook_lib();//只需调用函数
};
$scope.load_留言簿_lib=function(){
load('creatureCloud','v1',function(){
$scope.is_backend_ready=true;
$scope.message='Hello cats';
console.log('make it!');
$scope.$apply()//
angular.module('AnimalsApp', [])
  .controller('MainCtrl', ['$scope', '$window', function($scope, $window) {
    $scope.message = 'Hello';  
    $window.init = function() {
      $scope.$apply($scope.load_guestbook_lib);
    };
    $scope.load_guestbook_lib = function() {
      gapi.client.load('creatureCloud', 'v1', function() {
        $scope.is_backend_ready = true;
        $scope.message = 'Hello beautiful';  
        console.log('Made it!');
      }, 'http://localhost:8888/_ah/api');
    };
  }]);

function init() {
  window.init();
}
$window.init = function() {
  $scope.load_guestbook_lib(); //Just invoke the function
};
$scope.load_guestbook_lib = function() {
  gapi.client.load('creatureCloud', 'v1', function() {
    $scope.is_backend_ready = true;
    $scope.message = 'Hello cats';  
    console.log('Made it!');
    $scope.$apply(); //<-- Apply here
  }, 'http://localhost:8888/_ah/api');
};