Dev*_*man 28 javascript model-view-controller orm frameworks angularjs
在过去的几天里,我一直在尝试使用Angular JS,而我无法弄清楚的一件事是如何处理模型之间的关系.
我正在开发的项目有一个用户模型和一个帐户模型.我在我的数据库上设置了每个帐户都有一个名为'ownedBy'的字段,该字段是对拥有该帐户的用户的id的外键引用.
在Angular中,我在名为main.js的文件中进行了以下设置
var myApp = angular.module('myApp', ['ngResource']);
var Users = myApp.factory('Users', function($resource) {
var User = $resource('http://api.mydomain.ca/users/:id',
{id:'@id'},
{});
return User;
});
var Accounts = myApp.factory('Accounts', function($resource) {
var Accounts = $resource('http://api.mydomain.ca/accounts/:id',
{id:'@id'},
{});
return Accounts;
});
function UsersCtrl($scope, Users) {
$scope.users = Users.query();
}
function AccountsCtrl($scope, Accounts) {
$scope.accounts = Accounts.query();
}
Run Code Online (Sandbox Code Playgroud)
和以下模板
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>Angular Test</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="/bootstrap/css/bootstrap.min.css?v=2.2.1">
</head>
<body>
<div ng-app="myApp">
<div ng-controller="UsersCtrl">
<table class="table table-striped">
<thead>
<tr>
<th>ID</th>
<th>First Name</th>
<th>Last Name</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="user in users">
<td>{{user.id}}</td>
<td>{{user.firstName}}</td>
<td>{{user.lastName}}</td>
</tr>
</tbody>
</table>
</div>
<div ng-controller="AccountsCtrl">
<table class="table table-striped">
<thead>
<tr>
<th>ID</th>
<th>Owned By</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="account in accounts">
<td>{{account.id}}</td>
<td>{{account.ownedBy}}</td>
</tr>
</tbody>
</table>
</div>
</div>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.4/angular.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.4/angular-resource.min.js"></script>
<script src="/bootstrap/js/bootstrap.min.js?v=2.2.1"></script>
<script src="js/main.js"></script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
这是有效的.它从我的REST服务器中提取JSON资源并将其显示在表中.我需要采取的下一步是什么才能最终得到一个显示用户及其帐号的表格?(相当于数据库JOIN?)对于一对多的关系,有不同的方法吗?(即...一个帐户有很多交易)
谢谢您的帮助 :)
Jos*_*ler 25
$resource不包含任何处理服务器未处理的关系的方法,但它非常简单$http:
module.factory( 'UserService', function ( $http, $q ) {
return {
get: function getUser( id ) {
// We create our own promise to return
var deferred = $q.defer();
$http.get('/users/'+id).then( function ( user ) {
$http.get('/accounts/'+user.id).then( function ( acct ) {
// Add the account info however you want
user.account = acct;
// resolve the promise
deferred.resolve( user );
}, function getAcctError() { deferred.reject(); } );
}, function getUserError() { deferred.reject(); } );
return deferred.promise;
}
};
});
Run Code Online (Sandbox Code Playgroud)
然后在你的控制器中,你可以像任何其他承诺一样使用它:
UserService.get( $scope.userId ).then( function ( user ) {
$scope.user = user;
});
Run Code Online (Sandbox Code Playgroud)
它可用于您的模板!
<div>
User: "{{user.firstName}} {{user.lastName}}" with Acct ID "{{user.acct.id}}".
</div>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
16361 次 |
| 最近记录: |