javascript - 直接在 HTML 中来自 JSON 的 Angular 数据

标签 javascript angularjs

如果有一个直行 Controller :

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);
            });
        }

在 console.log 中显示身份不明。但是,如果我将其设置为 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])            
    });
}

然后您可以在 Controller 中使用 $scope.customer.email. 或在 html 中使用 {{customer.email}}

关于javascript - 直接在 HTML 中来自 JSON 的 Angular 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28376831/

相关文章:

angularjs - 将一个范围分配给另一个范围 Angularjs

angularjs - 如何在 Angular JS Controller 中使用 window.onload ?

javascript - 如何在svg元素上添加dragend事件

HTML 的 Javascript 函数只发生一次

javascript - 如何将我的注册详细信息存储在本地存储中?

javascript - 使用 Parse Server 返回的数据更新状态后,无法让 React 组件重新渲染

angularjs - 如何使用嵌套资源连接来自不同 URL 的 JSON 对象

javascript - Angular UI-Router 无法从其父状态解析子状态(使用动态 URL 参数)

javascript - 用户按下 'entrer' 后 AngularJs 过滤器搜索

javascript - jquery AJAX 中 XMLHttpRequest 对象的状态