Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/24.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 如何在angular js中将json数据推送到数组中_Angularjs_Json - Fatal编程技术网

Angularjs 如何在angular js中将json数据推送到数组中

Angularjs 如何在angular js中将json数据推送到数组中,angularjs,json,Angularjs,Json,如何将$scope.modules中的数据选择到treedata_avm数组中的标签中,而不是数组中的硬编码值 app.controller('treeController',['$http','$scope','dataService',function( $http,$scope,dataService){ $http.get('WS/tree').success(function(data,status){ $scope.modules=data;

如何将$scope.modules中的数据选择到treedata_avm数组中的标签中,而不是数组中的硬编码值

app.controller('treeController',['$http','$scope','dataService',function( $http,$scope,dataService){

    $http.get('WS/tree').success(function(data,status){
        $scope.modules=data;
        $scope.moduleSelected = $scope.modules[0].MODULE_NAME;
        $scope.moduleSelectedId = $scope.modules[0].ID;
        $scope.moduleSelectedParentId = $scope.modules[0].PARENT_ID;

     console.log($scope.modules);
     console.log($scope.moduleSelected);
     console.log($scope.moduleSelectedId);
     console.log($scope.moduleSelectedParentId);


    }).error(function(data){
        $scope.modules=data || "Request failed";
        console.log("Request failed "+ $scope.modules);
    });

    }]);
treedata_avm:

treedata_avm = [
  {
    label: 'Animal',
    children: [
      {
        label: 'Dog',

      }, {
        label: 'Cat',

      }, {
        label: 'Hippopotamus',

      }, {
        label: 'Chicken',
        children: ['White Leghorn', 'Rhode Island Red', 'Jersey Giant']
      }
    ]
  }]

$http
及其
success
promise resolve函数以当前格式返回数据:
response.data

这意味着在你的情况下,你应该首先

$http.get('WS/tree').success(function(response){
    $scope.modules = response.data;
    $scope.treedata_avm = response.data
}).error(function (){...})
如果要在视图上使用
treedata_avm
,则它应该像
$scope那样位于$scope上。如果不是,则只将其用作控制器内的变量

另外,我的建议是将所有这些
$http
逻辑放入一个提供者(工厂、服务、提供者)中,然后从那里调用它。 建议是使控制器尽可能精简,并将逻辑放入提供者(工厂、服务、提供者)中,从而使其可用


希望这有帮助。

您可以在字段上收听事件“onchange”,该事件绑定到$scope.moduleSelected,并在更改输入字段时更改json。但是我看不出来,为什么您需要这样的东西?

如何将$scope.modules中的数据选择到treedata_avm数组中的标签中,而不是数组中的硬编码值?