温泉 ng-repeat 和 ajax
Onsen ng-repeat and ajax
我的应用程序有一个大问题,我认为这是由异步调用引起的。
html部分是:
<ons-list ng-repeat="item in newsEventi.items">
<ons-list-header class="news_titolo_lista">{{item.categoria}}</ons-list-header>
<ons-list-item class="news_titolo">
({{item.titolo}})
</ons-list-item>
</ons-list>
js部分(控制器)是:
app.controller('NewsEventiController', ["$scope", function($scope)
{
//--------------------//
// private variables
//--------------------//
var _self = this;
var _items = [];
//--------------------//
// private functions
//--------------------//
// constructor
(function()
{
var url = "http://www.gaelico.net/app/test_json.php";
// IT RETURNS -> [{"titolo":"test","categoria":"cat1"},{"titolo":"test1","categoria":"cat2"}]
$.post(url, {data: JSON.stringify({regID: localStorage.getItem("regID")})},
function(json)
{
_items = JSON.parse(json);
$scope.$apply();
console.log("After: " + _items);
});
})();
//--------------------//
// public properties
//--------------------//
this.items = _items;
console.log("Before: " + this.items);
//************************************************************************//
// public methods
//************************************************************************//
}]);
我的问题是列表从不显示 2 个元素。我使用 $scope.$apply() 因为我认为这是更新列表的正确方法,但它似乎不起作用。
预先感谢您提出的每一个建议。
编辑
我尝试按照建议更改脚本,现在代码是
app.controller('NewsEventiController', ["$scope","$http", function($scope,$http)
{
//--------------------//
// private variables
//--------------------//
var _self = this;
$scope._items = [];
//--------------------//
// constructor
(function()
{
// the url returns: [{"titolo":"test","categoria":"cat1"},{"titolo":"test1","categoria":"cat2"}]
$http.get("https://www.gaelico.net/app/test_json.php")
.success(function(response) {
console.log(response);
// it shows in the console, correctly: "Object, object"
$scope._items = response;
});
})();
//--------------------//
// public properties
//--------------------//
console.log("Before: " + $scope._items); // empty
}]);
(在 ng-repeat 中,我输入 ng-repeat="item in newsEventi._items"
)
但是我仍然没有在列表中看到任何更新。
像这样声明项目:
$scope._items = []
你的 ngRepeat:
item in _items
您不需要调用 apply,我建议您查看 angulars http 模块而不是使用 jquery。
我的应用程序有一个大问题,我认为这是由异步调用引起的。
html部分是:
<ons-list ng-repeat="item in newsEventi.items">
<ons-list-header class="news_titolo_lista">{{item.categoria}}</ons-list-header>
<ons-list-item class="news_titolo">
({{item.titolo}})
</ons-list-item>
</ons-list>
js部分(控制器)是:
app.controller('NewsEventiController', ["$scope", function($scope)
{
//--------------------//
// private variables
//--------------------//
var _self = this;
var _items = [];
//--------------------//
// private functions
//--------------------//
// constructor
(function()
{
var url = "http://www.gaelico.net/app/test_json.php";
// IT RETURNS -> [{"titolo":"test","categoria":"cat1"},{"titolo":"test1","categoria":"cat2"}]
$.post(url, {data: JSON.stringify({regID: localStorage.getItem("regID")})},
function(json)
{
_items = JSON.parse(json);
$scope.$apply();
console.log("After: " + _items);
});
})();
//--------------------//
// public properties
//--------------------//
this.items = _items;
console.log("Before: " + this.items);
//************************************************************************//
// public methods
//************************************************************************//
}]);
我的问题是列表从不显示 2 个元素。我使用 $scope.$apply() 因为我认为这是更新列表的正确方法,但它似乎不起作用。
预先感谢您提出的每一个建议。
编辑 我尝试按照建议更改脚本,现在代码是
app.controller('NewsEventiController', ["$scope","$http", function($scope,$http)
{
//--------------------//
// private variables
//--------------------//
var _self = this;
$scope._items = [];
//--------------------//
// constructor
(function()
{
// the url returns: [{"titolo":"test","categoria":"cat1"},{"titolo":"test1","categoria":"cat2"}]
$http.get("https://www.gaelico.net/app/test_json.php")
.success(function(response) {
console.log(response);
// it shows in the console, correctly: "Object, object"
$scope._items = response;
});
})();
//--------------------//
// public properties
//--------------------//
console.log("Before: " + $scope._items); // empty
}]);
(在 ng-repeat 中,我输入 ng-repeat="item in newsEventi._items"
)
但是我仍然没有在列表中看到任何更新。
像这样声明项目:
$scope._items = []
你的 ngRepeat:
item in _items
您不需要调用 apply,我建议您查看 angulars http 模块而不是使用 jquery。