在Angularjs中的http调用之后设置范围值

时间:2016-01-06 08:26:39

标签: angularjs angularjs-scope

我有一个控制器,我在其中进行ajax调用。在成功回调中,我将一些值分配给$ scope变量,如下所示。

$http.get(GlobalService.getDomainUrl() +'/hosts/attr').success(function(data){
       //Now that we have made the ajax call we need to first get all the config values..
        $scope.classifications = _.allKeys(data);
        var config;
        _.each(data, function(e) {
            $scope.config = _.allKeys(e);
            console.log($scope.config);
            _.each(config, function(attr){
                $scope.attributes = attr;
            });
        });
    });

来自服务器的数据采用以下格式:

  {
    "my_config": {
    "cpu": [
      "2"
    ],
    "mem": [
      "4GB"
    ]
  },
  "network_config": {
    "my_config": [
      "ON",
      "OFF",
      "NONE"
    ],
    "os_config": [
      "LINUX",
      "MS",
      "OSX"
    ]
  }
}

在我的HTML代码中:

<div class="row">
  {{config}}
</div>

当我尝试使用{{config}}在我的html文件中使用值时,我看到一个空数组。在浏览器控制台上,我能够看到数据,因为我使用console.log语句进行打印。请让我知道我哪里出错了。

1 个答案:

答案 0 :(得分:0)

问题得到解决。 $scope.config设置正确,但由于从服务器返回的数组中的最后一项是空的,它覆盖了scope.config的值。