gpt4 book ai didi

javascript - angularjs 从 ng-repeat 获取对象到不同页面中的 Controller

转载 作者:行者123 更新时间:2023-11-29 21:56:48 25 4
gpt4 key购买 nike

我试过很多方法,services, $rootScope, broadcast...

我只想在用户选择的 ng-repeat 中获取对象的实例,并将其属性动态地列到新的 html 页面中。

有人可以告诉我这个案例的最佳解决方案吗?到目前为止,我尝试的一切都出错了。

我有两个 Controller ,一个用于第 1 页,另一个用于第 2 页,还有一个包含我的列表的服务。

app.service('selectedPost', function() {

var list = { ... };
var post;

return {
getPost: function() {
return post;
},
setPost: function(p) {
post = p;
}
});





app.controller('page2Ctrl', function($scope, selectedPost, $rootScope) {
//tried:
$scope.obj = selectedPost.getPost();

//tried
$scope.obj = {p : selectedPost.getPost() };

//tried:
$scope.obj = $rootScope.pobj;
});

app.controller('pageCtrl', function($scope, selectedPost, $rootScope) {
//I call this method in my page1 html and pass the ng-repeat object as parameter:
//tried:
$scope.selectPost = function(post) {
selectedPost.setPost(post);
};
//tried:
$scope.selectPost = function(post) {
$rootScope.pobj = post;
};

$scope.postList = selectedPost.getPostList();

$scope.totalItems = $scope.postList.length;
$scope.itemsPerPage = 6;
$scope.currentPage = 1;
$scope.maxSize = 5;

$scope.setPage = function (pageNo) {
$scope.currentPage = pageNo;
};

$scope.pageChanged = function() {
console.log('Page changed to: ' + $scope.currentPage);
};

$scope.pageCount = function () {
return Math.ceil($scope.postList.length / $scope.itemsPerPage);
};

$scope.filter = function(items, groupedBy) {
if (items) {
var finalItems = [],
thisGroup;
for (var i = 0; i < items.length; i++) {
if (!thisGroup) {
thisGroup = [];
}
thisGroup.push(items[i]);
if (((i+1) % groupedBy) === 0) {
finalItems.push(thisGroup);
thisGroup = null;
}
}
if (thisGroup) {
finalItems.push(thisGroup);
}
return finalItems;
}
};

$scope.$watch('currentPage + itemsPerPage', function() {
var begin = (($scope.currentPage - 1) * $scope.itemsPerPage),
end = begin + $scope.itemsPerPage;

$scope.filteredItems = $scope.postList.slice(begin, end);
$scope.listFilter = $scope.filter($scope.filteredItems, 2);
});
});

page1.html

<div ng-controller="pageCtrl">
<div class="row" ng-repeat="itemList in listFilter">
<div class="col-md-6" ng-repeat="post in itemList">
....
<input class="button" type="submit" value="More" ng-click="selectPost(post)"/>
....//redirect to page2.html
</div>
</div>
</div>

page2.html 文件:

<div ng-controller="page2Ctrl">{{obj.something}}</div>

谢谢!

最佳答案

您正在加载一个完全不同的页面(整页重新加载)并实例化一个全新的 Angular 应用程序。
您不能使用服务跨应用和重新加载传递状态,因为服务将在每次应用/重新加载时重新实例化。

我怀疑您正在尝试在 SPA 中实现路由(而不是传统的整页加载)。在这种情况下,您应该使用路由模块,例如 ngRouteui.router

例如:

<body ng-app="myApp">
...
<ng-view></ng-view>
...
<script src="lib/angular/angular.js"></script>
<script src="lib/angular/angular-route.js"></script>
</body>

angular.
module('myApp', ['ngRoute']).
config(function ($routeProvider) {
$routeProvider.
when('/path1', {
templateUrl: '...',
controller: '...'
}).
when('/path2', {...}).
...
}).
service('selectedPost', ...).
controller('...', ...).
...

另请参阅此 short demo .

关于javascript - angularjs 从 ng-repeat 获取对象到不同页面中的 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26146821/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com