【发布时间】:2014-04-19 15:59:03
【问题描述】:
我有一个 Angular 应用程序,它有两个视图:
1) 列表视图
2) 详细视图
当您从列表视图中单击缩略图时,您会转到详细视图,这是路线:
app.config(['$routeProvider',
function($routeProvider) {
$routeProvider.
when('/list', {
templateUrl: 'partials/list.html',
controller: 'ListCtrl',
}).
when('/list/:id', {
templateUrl: 'partials/detail.html',
controller: 'DetailCtrl',
}).
otherwise({
redirectTo: '/list'
});
}]);
现在'listCtrl'控制器中有一个函数loadmore用于加载
myControllers.controller('ListCtrl', ['$scope', '$location', 'Troll', '$http',
function ($scope, $location, Troll, $http) {
$scope.Trolls = Troll.query();
$scope.orderProp = 'popular';
$scope.fromData = {};
//console.log($scope.Trolls);
//this will be used to fetch the data in json which is defined in services.js
$scope.loadmore = function () {
jQuery.ajax({
url: 'trolls/trolls.php?troll_index=' + $('#main-content #item-list .sub-item').size(),
type: 'GET',
async: false,
data: {},
dataType: 'json',
success: function (response) {
if (response != null) {
$.each(response, function (index, item) {
$scope.Trolls.push({
UID: response[index].UID,
id: response[index].id,
popular: response[index].popular,
imageUrl: response[index].imageUrl,
name: response[index].name,
tags: response[index].tags,
category: response[index].category
});
});
}
},
complete: function () {},
error: function () {
console.log('Failed!');
}
});
$scope.text = 'Hello, Angular fanatic.';
$http.get('trolls/trolls.php?troll_id=' + Troll);
}
}]);
问题: 现在的问题是, 如果我转到详细视图并返回列表视图,单击 loadmore 后,我新加载的 div 不见了,我该如何保留它们??
【问题讨论】:
-
你试过 Angular 的 $templateCache 吗? docs.angularjs.org/api/ng/service/$templateCache
-
我读过 $templateCache 但它与我的问题有什么关系,你能用一些演示解释一下吗??
-
您使用
jQuery.ajax而不是$http的任何原因? -
“新加载的 div 不见了”。 ajax 请求再次运行后,它们会返回吗?
-
添加到@MichalCharemza 评论;你为什么要使用 jQuery?你已经有了 Angular!
标签: javascript angularjs savestate