数据绑定不适用于 AngularJS 中的 JSON
Data Binding not working on JSON in AngularJS
this.getData = function () {
$http.get("data/samples.json").then(function(d) {
console.log(angular.fromJson(d.data.cards)[0].title);
return angular.fromJson(d.data.cards);
});
};
this.cards = this.getData();
它将正确的值打印到控制台。
<p>{{main.cards[0].title}}</p>
什么都不显示,为什么?
returned 对象是异步的,而不是 returning 结果(不能 return 在回调中)。试试这个:
this.getData = function () {
$http.get("data/samples.json").then(function(d) {
this.cards = angular.fromJson(d.data.cards);
});
};
试试这个:
var me = this;
this.getData = function () {
$http.get("data/samples.json").then(function(d) {
console.log(angular.fromJson(d.data.cards)[0].title);
me.cards = angular.fromJson(d.data.cards);
});
};
this.getData = function () {
$http.get("data/samples.json").then(function(d) {
console.log(angular.fromJson(d.data.cards)[0].title);
return angular.fromJson(d.data.cards);
});
};
this.cards = this.getData();
它将正确的值打印到控制台。
<p>{{main.cards[0].title}}</p>
什么都不显示,为什么?
returned 对象是异步的,而不是 returning 结果(不能 return 在回调中)。试试这个:
this.getData = function () {
$http.get("data/samples.json").then(function(d) {
this.cards = angular.fromJson(d.data.cards);
});
};
试试这个:
var me = this;
this.getData = function () {
$http.get("data/samples.json").then(function(d) {
console.log(angular.fromJson(d.data.cards)[0].title);
me.cards = angular.fromJson(d.data.cards);
});
};