Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/400.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在Angular.js中连接模型_Javascript_Frameworks_Model_Angularjs - Fatal编程技术网

Javascript 在Angular.js中连接模型

Javascript 在Angular.js中连接模型,javascript,frameworks,model,angularjs,Javascript,Frameworks,Model,Angularjs,我刚刚开始使用Angular.js,我不知道如何将两个“模型”链接在一起。我的index.php文件中有以下代码 <div ng-controller="AccountCtrl"> <h2>Accounts</h2> <ul> <li ng-repeat="account in accounts"> <span>{{account.id}} {{account.owne

我刚刚开始使用Angular.js,我不知道如何将两个“模型”链接在一起。我的index.php文件中有以下代码

<div ng-controller="AccountCtrl">
    <h2>Accounts</h2>
    <ul>
        <li ng-repeat="account in accounts">
            <span>{{account.id}} {{account.ownedBy}}</span>
        </li>
    </ul>
</div>
<div ng-controller="TransactionCtrl">
    <h2>Transactions</h2>
    <ul>
        <li ng-repeat="transaction in transactions">
            <span>{{transaction.id}} {{transaction.timestamp}} {{transaction.amount}} {{transaction.description}} {{transaction.account}}</span>
        </li>
    </ul>
</div>
因此,在我的示例中,每个帐户都有许多事务,我想向我的帐户控制器添加一个函数,以根据事务计算帐户余额,但我不确定如何执行,因为它们位于不同的$scope中


在Angular中是否有这样做的方法,或者当我获取帐户时,我必须在服务器的JSON响应中返回“链接”事务信息?

我猜
帐户
保留
事务
,对吗?
那么我想,您可以创建一个
服务来管理帐户/交易数据。
将此服务注入两个控制器

module = angular.module('app', []);

module.factory('accountService', function($http) {
  var obj = {
    // handles http communication to/from server.
    // also has methods/getters/setters for data manipulation, etc.
  };
  return obj;
});

module.controller('AccountCtrl', function($scope, accountService) {
   // access accountService for the view-databind.
});

module.controller('TransactionCtrl', function($scope, accountService) {
   // access accountService for the view-databind.
});

由于您同时发出两个http请求,因此我将更改服务以将事务作为account对象的属性返回。这样,只需一次服务器调用,开销就会减少,而且您的数据将采用您需要的格式。我认为你的最后一个问题的想法是正确的

使用角度传感器是个不错的选择。如果你还没有找到它们,约翰·林德奎斯特(John Lindquest)在上发布了一组很棒的视频

module = angular.module('app', []);

module.factory('accountService', function($http) {
  var obj = {
    // handles http communication to/from server.
    // also has methods/getters/setters for data manipulation, etc.
  };
  return obj;
});

module.controller('AccountCtrl', function($scope, accountService) {
   // access accountService for the view-databind.
});

module.controller('TransactionCtrl', function($scope, accountService) {
   // access accountService for the view-databind.
});