如果有一个直行 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/