从内部函数更新函数变量外的JavaScript

时间:2017-10-17 16:16:01

标签: javascript angularjs closures

AttributeError                            Traceback (most recent call last)
<ipython-input-1-754aa36f8c5c> in <module>()
      8 import time
      9 from datetime import datetime
---> 10 from simple_resnet import *
     11 from hyper_parameters import *
     12 

~/deep-shopping/simple_resnet.py in <module>()
      6 '''
      7 import numpy as np
----> 8 from hyper_parameters import *
      9 
     10 #import tensorflow as tf

~/deep-shopping/hyper_parameters.py in <module>()
     31 
     32 ## Hyper-parameters about the model
---> 33 tf.app.flags.DEFINE_int('num_residual_blocks', 5, '''number of residual blocks in ResNet''')
     34 
     35 

AttributeError: module 'tensorflow.python.platform.flags' has no attribute 'DEFINE_int'

我试图更新getSubCategory函数内的数据变量,但数据变量仅在内部函数范围内更新,并且无法更新外部函数的数据变量,从而导致空对象,即{}初始化。提前谢谢!

1 个答案:

答案 0 :(得分:-1)

对于不熟悉承诺概念的开发人员而言,这个问题并不少见。 $ http服务返回由.then()方法实现的promise。所以你要做的是在调用.then()方法并履行承诺之前使用响应数据。

以下是您对更典型模式的服务建议重构:

angular.module('adminApp')
  .service('subCategoryFinder', ['$http', function($http) {
    this.getSubCategory = function() {
      return $http.get('/app/admin/category/category.json');
    };
  }])
  .controller('myController', ['$scope', 'subCategoryFinder', function($scope, subCategoryFinder) {
      subCategoryFinder.getSubCategory.then(function(response) {
        $scope.subCategories = response.data;
      });
    }
  }])