HTML中直接来自JSON的角度数据

Angular data from JSON directly in HTML

本文关键字:数据 JSON HTML      更新时间:2023-09-26

如果有直接控制器:

controller: function($scope, $http){
$http.get(templateSource+'/object.customer')
   .then(function(result){
      $scope = result.data;            
    });
}

我的object.customer文件看起来像:

[{"customer":{"name":"Bert","email":"bert@gmail.com"}}]

现在我想访问电子邮件,所以我做了(HTML)

{{customer.email}}

但这是不正确的,所以我的问题是,如何访问电子邮件?

更新
设置:

controller: function($scope, $http){
          $http.get(templateSource+'/object.customer')
           .then(function(result){
              $scope.customer = {};
              angular.extend($scope.customer,result.data[0]);
              console.log($scope.customer.email);
            });
        }

在控制台.log unidentified中给我。但是,如果我将其设置为console.log($scope.customer);,我会得到:

Object 0: 
  Object customer: 
   Object email: "bert@gmail.com"
          name: "Bert"

console.log(result.data)

[Object]
  0: Object
    customer: Object
        email: "bert@gmail.com"
        name: "Bert"

使用angular.extend

controller: function($scope, $http){
$http.get(templateSource+'/object.customer')
   .then(function(result){
         angular.extend($scope,result.data[0])            
    });
}

然后,您可以在控制器中使用$scope.customer.email.,也可以在html中使用{{customer.email}}