到目前为止我的基本代码:
function UsersCtrl($scope) {
$scope.users = [
{
"id": 1,
"usersName": "Matt Dance",
"favorites": {
"id":1,
"lodge_id":1
}
},
{
"id":2,
"usersName": "Jennifer Dance",
"favorites": {
"id":1,
"lodge_id":2
}
}
];
};
function LodgesCtrl($scope) {
$scope.lodges = [
{
id: 1,
lodgesName: 'Matt\'s Awesome Lodge',
lodgeOwnersName: 'Matthew D',
lodgeStreetAddress: '5555 J lane',
lodgeCity: 'Salt Lake city',
lodgeZip: '11111',
lodgeState: 'Utah'
},
{
id: 2,
lodgesName: 'Some Historic Lodge',
lodgeOwnersName: 'Santa Claus',
lodgeStreetAddress: '1 Clearwater Road',
lodgeCity: 'Grand Marais',
lodgeZip: '55555',
lodgeState: 'Minnesota'
},
{
id: 3,
lodgesName: 'Yellowstone Lodge',
lodgeOwnersName: 'The Feds',
lodgeStreetAddress: '1 Yellowstone Way',
lodgeCity: 'Yellowstone',
lodgeZip: '55555',
lodgeState: 'Wyoming'
},
{
id: 4,
lodgesName: 'Cool Lodge',
lodgeOwnersName: 'Awesome Guy',
lodgeStreetAddress: '1 awesome Road',
lodgeCity: 'Eagle Mountain',
lodgeZip: '55555',
lodgeState: 'Utah'
},
{
id: 5,
lodgesName: 'The last lodge',
lodgeOwnersName: 'Adam V',
lodgeStreetAddress: '444 Last Road',
lodgeCity: 'San Francisco',
lodgeZip: '55555',
lodgeState: 'California'
}
];
};
我希望用户能够看到他们所喜欢的小屋的小屋名称。例如,用户喜欢id为1的小屋。然后我可以循环浏览每个用户,并显示最喜欢的小屋的标题?基本上,我需要为他们收藏夹中的每个用户使用lodge_id
来显示小屋名称。
我的html到目前为止:
<div ng-controller="UsersCtrl">
<ul class="unstyled">
<li ng-repeat="user in users">
{{user.usersName}}<br>
</li>
</ul>
</div>
我是Angular(以及一般的JS框架)的新手,我不知道如何解决这个问题。我已经研究过像Ember这样的其他框架,但却无法以“简单”的方式解决它。
答案 0 :(得分:1)
最简单的解决方案是构造一个映射对象并将其绑定到$ rootScope。例如:
$rootScope.mapping = {
userId: [lodge1, lodge2...]
}
答案 1 :(得分:0)
我对您的代码进行了一些更改,现在它正在运行但我认为您需要以更好的方式构建数据,以便您可以更轻松地进行迭代。
你也可以在这里看到一个工作的傻瓜(http://plnkr.co/edit/DtBxzYulJGTQC0KJsh8M?p=preview)。
<!doctype html>
<html lang="en" ng-app="myApp">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.8/angular.min.js"></script>
<script>
var myApp = angular.module('myApp', []);
myApp.controller('UsersCtrl', ['$scope', function ($scope) {
$scope.users = [
{
"id": 1,
"usersName": "Matt Dance",
"favorites": {
"id":1,
"lodge_id":1
}
},
{
"id":2,
"usersName": "Jennifer Dance",
"favorites": {
"id":1,
"lodge_id":2
}
}
];
$scope.lodges = [
{
id: 1,
lodgesName: 'Matt\'s Awesome Lodge',
lodgeOwnersName: 'Matthew D',
lodgeStreetAddress: '5555 J lane',
lodgeCity: 'Salt Lake city',
lodgeZip: '11111',
lodgeState: 'Utah'
},
{
id: 2,
lodgesName: 'Some Historic Lodge',
lodgeOwnersName: 'Santa Claus',
lodgeStreetAddress: '1 Clearwater Road',
lodgeCity: 'Grand Marais',
lodgeZip: '55555',
lodgeState: 'Minnesota'
},
{
id: 3,
lodgesName: 'Yellowstone Lodge',
lodgeOwnersName: 'The Feds',
lodgeStreetAddress: '1 Yellowstone Way',
lodgeCity: 'Yellowstone',
lodgeZip: '55555',
lodgeState: 'Wyoming'
},
{
id: 4,
lodgesName: 'Cool Lodge',
lodgeOwnersName: 'Awesome Guy',
lodgeStreetAddress: '1 awesome Road',
lodgeCity: 'Eagle Mountain',
lodgeZip: '55555',
lodgeState: 'Utah'
},
{
id: 5,
lodgesName: 'The last lodge',
lodgeOwnersName: 'Adam V',
lodgeStreetAddress: '444 Last Road',
lodgeCity: 'San Francisco',
lodgeZip: '55555',
lodgeState: 'California'
}
];
}]);
</script>
</head>
<body>
<div ng-controller="UsersCtrl">
<ul>
<li ng-repeat="user in users">
{{user.usersName}}<br>
{{user.favorites.lodge_id}}<br>
<p ng-repeat="lodge in lodges | filter: { id: user.favorites.lodge_id }">
{{lodge.lodgesName}}
</p>
</li>
</ul>
</div>
</body>
</html>
答案 2 :(得分:0)
您应该使用服务。由于服务是单件,因此它们是在控制器之间共享内容的最简单方法。事实上,这是有角度的方式:
请勿将控制器用于:
跨控制器共享无状态或有状态代码 - 使用angular 相反,服务。